在Hibernate pojo中@lob的正确hibernate映射。我们正在使用hibernate映射。您能否告诉我相当于@lob注释 [英] proper hibernate mapping for @lob in Hibernate pojo. we are using hibernate mapping. can you please tell me equivalent for @lob annotation
问题描述
我们使用hibernate映射。在hibernate配置文件中,我们给出了type =blob和pojo class 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);
使用以下内容:
@Lob
@Basic(fetch = FetchType.LAZY)
@Column( name =CONTENUTO_BLOB,nullable = true)
public Blob getContenutoBlob()
{
return contenutoBlob;
}
注释 @Lob
表示它是一个Lob列; @Basic(fetch = FetchType.LAZY)
指示加载实体而不将Lob加载到内存中;你只能在真正需要的时候才能访问这个升级包
$ b
UPDATE
长我不使用XML,但如果我没有错,你可以使用这样的东西:
< property name = contenutoBlobtype =org.hibernate.type.BinaryTypelazy =true>
< / property>
更新2
在任何情况下,我从来没有使用 hibernateTemplate
通过使用hibernateTemplate,您可以访问hibernate session
通常我执行以下操作:
保存Blob方法
public void saveAllegato(InputStream fileIn,long lunghezza)throws DbException
{
// Dai test effettuati,quando siamo col portale attivo bisogna non chioudere
// mai lo stream
boolean closeStream = false;
try
{
// sf是SessionFactory
Session sessione = sf.getCurrentSession();
Blob blob = null;
if(null!= fileIn)
{
blob = Hibernate.getLobCreator(sessione).createBlob(fileIn,lunghezza);
}
AllegatoModel entity = new AllegatoModel();
//设置其他字段
if(blob!= null)
{
entity.setContenutoBlob(blob);
}
//保存对象
sessione.saveOrUpdate(entity);
catch(Exception e)
{
String message =Errore nel salvataggio della entity+ entity +;+ e.getMessage();
logger.error(message,e);
抛出新的PinfGpDbException(消息);
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());
}
}
}
}
< getBuffer方法
public void writeAllegatoFile(Long id,OutputStream out)抛出PinfGpDbException
{
StopWatch sw = new StopWatch(SCRITTURA FILE CON ID [+ id +] SU OUTPUTSTREAM);
InputStream是= null;
尝试
{
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();
列出< 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(是,out);
sw.stop();
StopWatchUtils.printStopWatchInfo(sw,logger,false,SCRITTURA FILE TERMINATA CORRETTAMENTE);
$ b catch(例外e)
{
String message =Errore nel recupero dell'allegato con ID + id;
logger.error(message,e);
抛出新的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()); ($!
$ b 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());
}
}
}
}
}
We are using hibernate mapping. In hibernate configuration file we have given type="blob" and pojo class getBlob and setBlob methods we have. Apart from this we need to have @lob right. what is equivalent for lob in hibernate mapping
@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);
}
In my model I'm using the following:
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "CONTENUTO_BLOB", nullable = true)
public Blob getContenutoBlob()
{
return contenutoBlob;
}
The annotation @Lob
indicates that it's a Lob column; the @Basic(fetch=FetchType.LAZY)
indicates to load the entity without loading the Lob in memory; you can access to the lob only when you really need
UPDATE
Long time I don't use XML but if I'm not wrong you may use something like this:
<property name="contenutoBlob" type="org.hibernate.type.BinaryType" lazy="true">
</property>
UPDATE 2
I never used hibernateTemplate
in any case also by using hibernateTemplate you can access to the hibernate session
Usually I do the following:
Save Blob method
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());
}
}
}
}
GET BLOB METHOD
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());
}
}
}
}
}
这篇关于在Hibernate pojo中@lob的正确hibernate映射。我们正在使用hibernate映射。您能否告诉我相当于@lob注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!