failed to lazily initialize a collection of role
During the development of the STRUTS2.0+HIBERNATE3.2+SPRING2.5 project, I encountered failed to lazily initialize a collection of role:xxxxxx, no session or Sessio N was closed this unusual trouble, initially to find information on the Internet, got some of the following workarounds:
1, is the corresponding one-to-many of the two columns Lazy=true changed to Lazy=false can be;
2, for the query if the use of Xxx.load (Class,id) is changed to Xxx,get (Class,id);
3. In the Web. xml file, add:
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
Org.springframework.orm.hibernate3.support.opensessioninviewfilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>false</param-value
</init-param>
<!--this--<init-param> must add or it will probably error: Org.springframework.dao.InvalidDataAccessApiUsageException:Write Operations is not allowed in read-only mode (flushmode.never)-Turn your Session into Flushmode.auto or remove ' readOnly ' Marker from transaction definition
-
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.mmg</url-pattern>
</filter-mapping>
The above method one by one test, to the results are the same, the same anomalies, and then have to find information on the Internet, and their own efforts to think, and later learned that spring can solve the problem well, Spring Framework for Hibernate Lazy Loading and DAO the integration of the model provides a convenient solution. For those unfamiliar with Springwith the Hibernate integration with the people I won't be here to discuss too much detail but I suggest you to understand Hibernate with the Spring Integrated data access. With aWeb application As an example, Spring provides a Opensessioninviewfilter and the Opensessioninviewinterceptor . We are free to choose a class to implement the same functionality. The only difference between these two methods is that Interceptor in the Spring container, and is configured to run in the Web in the context of the application, and Filter in the Springpreviously run and configured in the Web. XML the. Whichever they are, they are asking to open the current session with the current (database) thread binding Hibernate session. Once bound to the thread, this open Hibernate the session can be DAO used transparently in the implementation class. This session remains open for a view that delays loading a value object in the database. Once this logical view has been completed, Hibernate the session will be the doFilter method of Filter or Interceptor is closed in the Posthandle method. Solve this problem with springand you don't have to set lazy to false to improve performance.
<FILTER>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
< Filter-name>hibernatefilter</filter -name>
<filter-class>
org.springframework.orm.hibernate3.support.opensessioninviewfilter
< /filter-class>
</filter>
< filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
At the beginning, the configuration is arbitrarily added to the end of Web. XML, found or not, and later through the network to understand the problem of filter order, it should be:
Opensessioninviewfilter
Actioncontextcleanup
Filterdispatcher
Order, and finally adjust the order of the filters to solve some problems.
Note: Sometimes some pages or some other action will not be filtered, can be modified as follows:
<url-pattern>/*</url-pattern>
Failed to lazily initialize a collection of role: