Spring + Hibernate 懒加载

4

我有一个问题,涉及到org.hibernate.LazyInitializationException的错误:无法延迟初始化角色集合。

如何在gwt + spring + hibernate中实现懒加载?

以下是我的appContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="   http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>
    <context:annotation-config/>
    <context:component-scan base-package="com.yeah.server.*"/>
    <aop:aspectj-autoproxy/>
    <!--Mysql database connection info-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://192.168.1.4:3306/YeaH"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>
    <!-- Hibernate -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingResources">
            <list>
                <value>com/yeah/shared/model/User.hbm.xml</value>
                <value>com/yeah/shared/model/Comment.hbm.xml</value>
                <value>com/yeah/shared/model/Album.hbm.xml</value>
                <value>com/yeah/shared/model/Picture.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
    <!--HIbernate session management
 <bean name="openSessionInViewInterceptor"
     class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
 <property name="sessionFactory" ref="sessionFactory"/>
 <property name="singleSession" value="false"/>
 </bean>
 -->
    <!-- a PlatformTransactionManager is still required -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

请参见http://www.gwtproject.org/articles/using_gwt_with_hibernate.html。 - David Mann
2个回答

10

出现异常的原因是您尝试在被关闭的Hibernate会话中加载的领域对象上访问延迟加载属性。

通常解决此问题的方法是使用Spring OpenSessionInViewFilter。这将基本上将Hibernate会话限定在HTTP请求中。然后,发生在该HTTP请求/响应周期内的任何属性访问都将处于该会话的范围内。

在web.xml中进行配置如下:

<filter>
    <filter-name>Spring OpenSessionInViewFilter</filter-name>
    <filter-class>
        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
</filter>

希望这可以帮助到你。


我使用了你的代码,但现在又遇到了另一个问题,也许你可以帮忙解决。om.google.gwt.user.client.rpc.SerializationException: Type 'org.hibernate.collection.PersistentList'未包含在此SerializationPolicy可序列化的类型集中,或其Class对象无法加载。出于安全考虑,此类型将不会被序列化:instance = [] at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:614) - karq
这似乎不是一个Hibernate问题。恐怕我从未使用过GWT。也许提出一个新问题会得到答案。 - Alex Barnes
这绝对是一个 Hibernate + GWT 的问题。请参阅 http://www.gwtproject.org/articles/using_gwt_with_hibernate.html。 - David Mann
你的 POJO bean 是否实现了 Serializable 接口? - Godwin

0
通常这种问题出现在Hibernate的双向映射中,为此您可以在子级别(ManyToOne)上使用@jsonbackreference,在父级别(OneToMany)上使用@jsonmanagedfrence,并且必须添加EAGER的获取类型。

这个问题与_JSON_无关。 - Tobias Liefke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接