Spring,Hibernate,Blob懒加载 [英] Spring, Hibernate, Blob lazy loading

查看:119
本文介绍了Spring,Hibernate,Blob懒加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要Hibernate中懒惰blob加载的帮助。
我的web应用程序中有这些服务器和框架:MySQL,Tomcat,Spring和Hibernate。
$ b $ p <数据库配置的一部分。

 < bean id =dataSourceclass =com.mchange.v2.c3p0.ComboPooledDataSourcedestroy-method =close> 
< property name =uservalue =$ {jdbc.username}/>
< property name =passwordvalue =$ {jdbc.password}/>
< property name =driverClassvalue =$ {jdbc.driverClassName}/>
< property name =jdbcUrlvalue =$ {jdbc.url}/>

< property name =initialPoolSize>
< value> $ {jdbc.initialPoolSize}< /值>
< / property>
< property name =minPoolSize>
< value> $ {jdbc.minPoolSize}< /值>
< / property>
< property name =maxPoolSize>
< value> $ {jdbc.maxPoolSize}< /值>
< / property>
< property name =acquireRetryAttempts>
< value> $ {jdbc.acquireRetryAttempts}< /值>
< / property>
< property name =acquireIncrement>
< value> $ {jdbc.acquireIncrement}< /值>
< / property>
< property name =idleConnectionTestPeriod>
< value> $ {jdbc.idleConnectionTestPeriod}< /值>
< / property>
< property name =maxIdleTime>
< value> $ {jdbc.maxIdleTime}< /值>
< / property>
< property name =maxConnectionAge>
< value> $ {jdbc.maxConnectionAge}< /值>
< / property>
< property name =preferredTestQuery>
< value> $ {jdbc.preferredTestQuery}< /值>
< / property>
< property name =testConnectionOnCheckin>
< value> $ {jdbc.testConnectionOnCheckin}< /值>
< / property>
< / bean>


< bean id =lobHandlerclass =org.springframework.jdbc.support.lob.DefaultLobHandler/>

< bean id =sessionFactoryclass =org.springframework.orm.hibernate3.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =configLocationvalue =/ WEB-INF / hibernate.cfg.xml/>
< property name =configurationClassvalue =org.hibernate.cfg.AnnotationConfiguration/>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> $ {hibernate.dialect}< / prop>
< /道具>
< / property>
< property name =lobHandlerref =lobHandler/>
< / bean>

< tx:annotation-driven transaction-manager =txManager/>

< bean id =txManagerclass =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =sessionFactory/>
< / bean>

实体类的一部分



<$ p $
@Basic(fetch = FetchType.LAZY)
@Column(name =BlobField,columnDefinition =LONGBLOB)
@Type(type =org.springframework.orm.hibernate3.support.BlobByteArrayType)
private byte [] blobField;

问题描述。我试图在网页上显示与文件相关的记录,这些记录保存在MySQL数据库中。如果数据量很小,所有工作都很好。但数据量很大我收到一个错误 java.lang.OutOfMemoryError:Java堆空间
我尝试在blobFields中写入空值每排桌子。在这种情况下,应用程序工作正常,内存不会消失。我得出结论,标记为懒惰( @Basic(fetch = FetchType.LAZY))的blob字段不是懒惰的,实际上!

解决方案

我很困惑。 Emmanuel Bernard在 ANN-418 中写道 @ Lob 默认是懒惰的(即,甚至不需要使用 @Basic(fetch = FetchType.LAZY)注释)。

有些用户报告,延迟加载 @Lob 不适用于所有的驱动程序/数据库

有些用户报告说它使用字节码工具(javassit?cglib?)。



但我在文档中找不到任何明确的参考资料。 推荐解决方法是使用假一对一映射而不是属性。从现有类中移除LOB字段,创建引用同一个表,相同主键以及只有必要的LOB字段作为属性的新类。指定映射为一对一,fetch =select,lazy =true。只要你的父母对象仍然在你的会话中,你应该得到你想要的东西。(只需将其转换为注释)。


I need help with lazy blob loading in Hibernate. I have in my web application these servers and frameworks: MySQL, Tomcat, Spring and Hibernate.

The part of database config.

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="driverClass" value="${jdbc.driverClassName}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>

    <property name="initialPoolSize">
        <value>${jdbc.initialPoolSize}</value>
    </property>
    <property name="minPoolSize">
        <value>${jdbc.minPoolSize}</value>
    </property>
    <property name="maxPoolSize">
        <value>${jdbc.maxPoolSize}</value>
    </property>
    <property name="acquireRetryAttempts">
        <value>${jdbc.acquireRetryAttempts}</value>
    </property>
    <property name="acquireIncrement">
        <value>${jdbc.acquireIncrement}</value>
    </property>
    <property name="idleConnectionTestPeriod">
        <value>${jdbc.idleConnectionTestPeriod}</value>
    </property>
    <property name="maxIdleTime">
        <value>${jdbc.maxIdleTime}</value>
    </property>
    <property name="maxConnectionAge">
        <value>${jdbc.maxConnectionAge}</value>
    </property>
    <property name="preferredTestQuery">
        <value>${jdbc.preferredTestQuery}</value>
    </property>
    <property name="testConnectionOnCheckin">
        <value>${jdbc.testConnectionOnCheckin}</value>
    </property>
</bean>


<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="/WEB-INF/hibernate.cfg.xml" />
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        </props>
    </property>
    <property name="lobHandler" ref="lobHandler" />
</bean>

<tx:annotation-driven transaction-manager="txManager" />

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

The part of entity class

@Lob
@Basic(fetch=FetchType.LAZY)
@Column(name = "BlobField", columnDefinition = "LONGBLOB")
@Type(type = "org.springframework.orm.hibernate3.support.BlobByteArrayType")
private byte[] blobField;

The problem description. I'm trying to display on a web page database records related to files, which was saved in MySQL database. All works fine if a volume of data is small. But the volume of data is big I'm recieving an error java.lang.OutOfMemoryError: Java heap space I've tried to write in blobFields null values on each row of table. In this case, application works fine, memory doesn't go out of. I have a conclusion that the blob field which is marked as lazy (@Basic(fetch=FetchType.LAZY)) isn't lazy, actually!

解决方案

I'm confused. Emmanuel Bernard wrote in ANN-418 that @Lob are lazy by default (i.e. you don't even need to use the @Basic(fetch = FetchType.LAZY) annotation).

Some users report that lazy loading of a @Lob doesn't work with all drivers/database.

Some users report that it works when using bytecode instrumentation (javassit? cglib?).

But I can't find any clear reference of all this in the documentation.

At the end, the recommended workaround is to use a "fake" one-to-one mappings instead of properties. Remove the LOB fields from your existing class, create new classes referring to the same table, same primary key, and only the necessary LOB fields as properties. Specify the mappings as one-to-one, fetch="select", lazy="true". So long as your parent object is still in your session, you should get exactly what you want. (just transpose this to annotations).

这篇关于Spring,Hibernate,Blob懒加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆