我们正在使用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);
}
答案 0 :(得分:2)
在我的模型中,我使用以下内容:
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "CONTENUTO_BLOB", nullable = true)
public Blob getContenutoBlob()
{
return contenutoBlob;
}
注释@Lob
表示它是一个Lob列; @Basic(fetch=FetchType.LAZY)
表示加载实体而不将Lob加载到内存中;只有在你真正需要的时候才能访问吊球
<强>更新强>
很长一段时间我都不使用XML,但如果我没有错,你可以使用这样的东西:
<property name="contenutoBlob" type="org.hibernate.type.BinaryType" lazy="true">
</property>
更新2
我在任何情况下都没用hibernateTemplate
也可以通过使用hibernateTemplate来访问hibernate会话
通常我会做以下事情:
保存Blob方法
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());
}
}
}
}
获取BLOB方法
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());
}
}
}
}
}
答案 1 :(得分:0)
使用basic
标记并在其中添加lob
标记:
<basic name="lobcolumn">
<column name="lob_column"/>
<lob/>
</basic>
答案 2 :(得分:0)
类型是blob。它使用Blob数据类型映射到hibernate pojo。它们将匹配并保存您的图像。
在hbm.xml中
property name =“contentualblob”type =“blob”