我对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>
答案 0 :(得分:10)
发生异常是因为您在加载的hibernate会话关闭之后尝试访问域对象上的延迟加载属性。
解决此问题的常用方法是使用Spring OpenSessonInViewFilter。这实际上将您的hibernate会话范围限定为您的HTTP请求。然后,在该HTTP请求/响应周期内发生的任何属性访问都将在该会话的范围内。
您可以在web.xml中对其进行如下配置:
<filter>
<filter-name>Spring OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
希望这有帮助。
答案 1 :(得分:0)
一般来说,这种类型的问题出现在hibernate中的双向映射中,因为你在子端(manytoone)上使用@jsonbackrefrence,在父端(onetomany)使用@jsonmanagedfrence,并且必须添加获取类型EAGER。