Hibernate pojo中@lob的正确hibernate映射。我们正在使用hibernate映射。你能告诉我@lob注释的等价物吗?

问题描述 投票:2回答:3

我们正在使用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);
}
java spring hibernate bytea
3个回答
2
投票

在我的模型中,我使用以下内容:

@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "CONTENUTO_BLOB", nullable = true)
public Blob getContenutoBlob()
{
    return contenutoBlob;
}

注释@Lob表示它是一个Lob列; @Basic(fetch=FetchType.LAZY)表示加载实体而不将Lob加载到内存中;只有在你真正需要时才可以访问吊球

UPDATE

很长一段时间我不使用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());
                }
            }
        }
    }
}

0
投票

使用basic标签并在里面添加lob标签:

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

0
投票

类型是blob。它映射到具有Blob数据类型的hibernate pojo。它们将匹配并且它也会保存您的图像。

在hbm.xml中

property name =“contentualblob”type =“blob”

© www.soinside.com 2019 - 2024. All rights reserved.