Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java hibernate pojo中@lob的正确hibernate映射。我们正在使用hibernate映射。您能告诉我@lob注释的等效值吗_Java_Spring_Hibernate_Bytea - Fatal编程技术网

Java hibernate pojo中@lob的正确hibernate映射。我们正在使用hibernate映射。您能告诉我@lob注释的等效值吗

Java hibernate pojo中@lob的正确hibernate映射。我们正在使用hibernate映射。您能告诉我@lob注释的等效值吗,java,spring,hibernate,bytea,Java,Spring,Hibernate,Bytea,我们正在使用hibernate映射。在hibernate配置文件中,我们给出了type=“blob”和pojo类getBlob和setBlob方法。除此之外,我们还需要有@lob权限。hibernate映射中的lob等效于什么 @Override public Hospital getHospital(long hospId, String hospitalName) { Hospital hos= hibernateTemplate.find("from Hospital h

我们正在使用hibernate映射。在hibernate配置文件中,我们给出了type=“blob”和pojo类getBlob和setBlob方法。除此之外,我们还需要有@lob权限。hibernate映射中的lob等效于什么

@Override
public Hospital  getHospital(long hospId, String hospitalName) {
    Hospital hos= hibernateTemplate.find("from Hospital
    hos where hos.id = ? and hos.name = ? ", hospId,hospitalName);          
}
@Transactional
public void saveHospital(Hosipital hos) {
    Blob blob = Hibernate.getLobCreator(hibernateTemplate.getSessionFactory().getCurrentSession()).createBlob(hos.getContent());
    hos.setHospitalImage(blob);
    hibernateTemplate.saveOrUpdate("Hospital", hos);
}

使用
basic
标记并将
lob
标记添加到:

    <basic name="lobcolumn">
        <column name="lob_column"/>
        <lob/>
    </basic>

在我的模型中,我使用以下方法:

@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "CONTENUTO_BLOB", nullable = true)
public Blob getContenutoBlob()
{
    return contenutoBlob;
}
注释
@Lob
表示它是一个Lob列;
@Basic(fetch=FetchType.LAZY)
指示加载实体而不加载内存中的Lob;只有在确实需要时才能访问lob

更新

很长一段时间我不使用XML,但如果我没有错,您可以使用以下内容:

 <property name="contenutoBlob" type="org.hibernate.type.BinaryType" lazy="true">
 </property>
获取BLOB方法

public void writeAllegatoFile(长id,OutputStream out)抛出PinfGpDbException
{
秒表sw=新秒表(“SCRITTURA文件CON ID[“+ID+”]SU输出流”);
InputStream=null;
尝试
{
软件启动(“Recepero文件数据库”);
DetachedCriteria=DetachedCriteria.forClass(AllegatoModel.class);
添加(Property.forName(“id”).eq(id));
ProjectionList pl=Projections.ProjectionList();
pl.add(Projections.property(“contenutoBlob”),“contenutoBlob”);
pl.add(项目、财产(“id”),“id”);
标准:设定投影(pl);
标准.setResultTransformer(Transformers.aliasToBean(AllegatoModelBlobTo.class));
Session sessione=sf.getCurrentSession();
列表结果=criteria.getExecutableCriteria(sessionOne.List();
sw.stop();
StopWatchUtils.printStopWatchInfo(sw,logger,false,“查询Esegita Corretdamente.RECORD TROVATI”+结果.size()+恢复Corretdamente”);
如果(result.size()>1)
{
抛出新的IllegalStateException(“不可能的proseguire trovati”+result.size()+“记录每l'ID”+ID);
}
AllegatoModelBlobDto theObj=result.get(0);
if(theObj!=null)
{
Blob contenuto=theObj.getContenutoBlob();
if(contenuto!=null)
{
sw.start(“SCRITTURA文件SU输出流”);
is=contenuto.getBinaryStream();
IOUtils.copy(is,out);
sw.stop();
printStopWatchInfo(sw、logger、false,“SCRITTURA文件Terminta Corretdamente”);
}
}
}
捕获(例外e)
{
String message=“error nel recepero dell'allegato con ID”+ID;
记录器。错误(消息,e);
抛出新的PinfGpDbException(消息,e);
}
最后
{
if(sw.isRunning())
{
sw.stop();
printStopWatchInfo(软件,记录器,真,“可能的错误NELLA SCRITTURA DEL文件”);
}
如果(is!=null)
{
尝试
{
is.close();
}
捕获(例外e)
{
//Stampo lo stacktrace solo quando il log ha livello di debug
if(logger.isDebugEnabled())
{
debug(“Errore nella chiusura del file input stream”,e);
}
else if(logger.iswarnabled())
{
logger.debug(“Errore nella chiusura del file input stream;”+e.getMessage());
}
}
}
if(out!=null)
{
尝试
{
out.close();
}
捕获(例外e)
{
//Stampo lo stacktrace solo quando il log ha livello di debug
if(logger.isDebugEnabled())
{
调试(“Errore nella chiusura dell’output stream”,e);
}
else if(logger.iswarnabled())
{
logger.debug(“Errore nella chiusura dell'output stream;”+e.getMessage());
}
}
}
}
}

类型为blob。它映射到具有Blob数据类型的hibernate pojo。两者都将匹配,并保存您的图像

在hbm.xml u中


property name=“contentualblob”type=“blob”

我没有使用hibernate注释。假设您正在使用hibernate映射,那么如何在hibernate映射中激活@lob?谢谢您的更新。但我没有使用二进制类型。我使用的是blob类型,如图所示。在后期,HIbernate中没有blob类型。type
org.hibernate.type.BinaryType
告诉hibernate将此属性作为二进制对象进行管理;然后,您可以在ode中使用java.sql.Blob,或者RDBMSwe的实现使用type=“java.sql.Blob”。二进制类型在图像处理方面有限制。如果使用blob类型,则在保存到数据库时会扩展到2gb。二进制类型不会扩展到2gb。根据postgres documentations,此时尝试使用
type=org.hibernate.type.BlobType
您可以在这里查看所有hibernate类型,因此在pojo类中,我们需要获取blob和setBlob?不需要使用setLOb和getLob?我使用的是postgres db。列数据类型为oid
public void saveAllegato(InputStream fileIn, long lunghezza) throws DbException
    {
        //Dai test effettuati, quando siamo col portale attivo bisogna non chiudere
        //mai lo stream
        boolean closeStream = false;
        try
        {
            //sf is the SessionFactory
            Session sessione = sf.getCurrentSession();
            Blob blob = null;
            if (null != fileIn)
            {
                blob = Hibernate.getLobCreator(sessione).createBlob(fileIn, lunghezza);
            }
            AllegatoModel entity = new AllegatoModel();
            //Set the other fields
            if( blob != null )
            {

                entity.setContenutoBlob(blob);
            }
            // Save object
            sessione.saveOrUpdate(entity);
        }
        catch (Exception e)
        {
            String message = "Errore nel salvataggio della entity " + entity + "; " + e.getMessage();
            logger.error(message, e);
            throw new PinfGpDbException(message);
        }
        finally
        {
            if (fileIn != null)
            {
                try
                {
                    fileIn.close();
                }
                catch (Exception e)
                {
                    //Stampo lo stacktrace solo quando il log ha livello di debug
                    if( logger.isDebugEnabled() )
                    {

                        logger.debug("Errore nella chiusura del file input stream ", e);
                    }
                    else if( logger.isWarnEnabled() )
                    {
                        logger.debug("Errore nella chiusura del file input stream; "+e.getMessage());
                    }
                }
            }
        }
public void writeAllegatoFile(Long id, OutputStream out) throws PinfGpDbException
{
    StopWatch sw = new StopWatch("SCRITTURA FILE CON ID ["+id+"] SU OUTPUTSTREAM");
    InputStream is = null;
    try
    {
        sw.start("RECUPERO FILE DA DB");
        DetachedCriteria criteria = DetachedCriteria.forClass(AllegatoModel.class);
        criteria.add(Property.forName("id").eq(id));
        ProjectionList pl = Projections.projectionList();
        pl.add(Projections.property("contenutoBlob"), "contenutoBlob");
        pl.add(Projections.property("id"), "id");
        criteria.setProjection(pl);
        criteria.setResultTransformer(Transformers.aliasToBean(AllegatoModelBlobDto.class));
        Session sessione = sf.getCurrentSession();
        List<AllegatoModelBlobDto> result = criteria.getExecutableCriteria(sessione).list();
        sw.stop();
        StopWatchUtils.printStopWatchInfo(sw, logger, false, "QUERY ESEGUITA CORRETTAMENTE. RECORD TROVATI "+result.size()+" RECUPERATO CORRETTAMENTE");
        if (result.size() > 1)
        {
            throw new IllegalStateException("Impossibile proseguire trovati " + result.size() + "record per l'ID " + id);
        }
        AllegatoModelBlobDto theObj = result.get(0);
        if (theObj != null)
        {
            Blob contenuto = theObj.getContenutoBlob();

            if (contenuto != null)
            {
                sw.start("SCRITTURA FILE SU OUTPUT STREAM");
                is = contenuto.getBinaryStream();
                IOUtils.copy(is, out);
                sw.stop();
                StopWatchUtils.printStopWatchInfo(sw, logger, false, "SCRITTURA FILE TERMINATA CORRETTAMENTE");
            }
        }

    }
    catch (Exception e)
    {
        String message = "Errore nel recupero dell'allegato con ID " + id;
        logger.error(message, e);
        throw new PinfGpDbException(message, e);
    }
    finally
    {
        if(sw.isRunning())
        {
            sw.stop();
            StopWatchUtils.printStopWatchInfo(sw, logger, true, "POSSIBILE ERRORE NELLA SCRITTURA DEL FILE");
        }
        if( is != null )
        {
            try
            {
                is.close();
            }
            catch (Exception e)
            {
                //Stampo lo stacktrace solo quando il log ha livello di debug
                if( logger.isDebugEnabled() )
                {

                    logger.debug("Errore nella chiusura del file input stream ", e);
                }
                else if( logger.isWarnEnabled() )
                {
                    logger.debug("Errore nella chiusura del file input stream; "+e.getMessage());
                }
            }
        }
        if( out != null )
        {
            try
            {
                out.close();
            }
            catch (Exception e)
            {
                //Stampo lo stacktrace solo quando il log ha livello di debug
                if( logger.isDebugEnabled() )
                {

                    logger.debug("Errore nella chiusura dell'output stream ", e);
                }
                else if( logger.isWarnEnabled() )
                {
                    logger.debug("Errore nella chiusura dell'output stream; "+e.getMessage());
                }
            }
        }
    }
}