Spring + sleeping lazy choice

I have a problem with org.hibernate.LazyInitializationException: failed to lazily initialize the role collection.

How to implement lazy fetching with gwt + spring + hibernate?

Here is my 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> 
+4
source share
2 answers

An exception occurs because you are trying to access the lazy loaded property of a domain object after the hibernate session into which it was loaded has been closed.

A common way to fix this is to use Spring OpenSessonInViewFilter. This greatly facilitates the hibernate session for your HTTP request. Then any access to the properties that occurs in this HTTP request / response cycle will be within the scope of this session.

You configure it in your web.xml as follows:

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

Hope this helps.

+10
source

usually this type of problem occurs when bidirectional matching in sleep mode, for this you use @jsonbackrefrence on the child side (manytoone) and @jsonmanagedfrence on the parent side (onetomany) and must add an EACHER type.

0
source

All Articles