Lazy loading exception: org.hibernate.LazyInitializationException: could not initialize proxy - no

Hibernate's Lazy initialization 1: n relationship, must be guaranteed to be in the same Session set for internal use this relationship, or Hiernate will throw an exception.

Two processing methods:
First, it is delay loaded questions to all pojo class linked, in the hibernate.cfg.xml file. Generally many-to-one in, set the tag in the set lazy = "false".

Second, with OpenSessionInViewFilter filter, attention hibernateFilter filters and filter struts2 mapping the order. To configure the transaction processing at the same time, otherwise it will result in session in read-only state do not modify, delete action.
That is configured in the web.xml file as follows:
<! - Spring ApplicationContext loaded configuration file directory. ->
<context-param>
<param-name> contextConfigLocation </ param-name>
<param-value> classpath *: spring / applicationContext.xml </ param-value>
</ Context-param>

<! - Lazy problem solving ->
<filter>
<filter-name> hibernateFilter </ filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</ Filter-class>
</ Filter>

<! - Inherited Struts2 of FilterDispatcher class, with GBK encoding setting features such as the action with the struts2 filter function. ->
<filter>
<filter-name> struts2 </ filter-name>
<filter-class> com.iman.nrms.opm.web.common.FilterDispatcher
</ Filter-class>
<init-param>
<param-name> encoding </ param-name>
<param-value> GBK </ param-value>
</ Init-param>
</ Filter>
<filter>
<filter-name> struts-cleanup </ filter-name>
<filter-class> org.apache.struts2.dispatcher.ActionContextCleanUp
</ Filter-class>
</ Filter>
<filter-mapping>
<filter-name> hibernateFilter </ filter-name>
<url-pattern> / * </ url-pattern>
</ Filter-mapping>
<filter-mapping>
<filter-name> struts2 </ filter-name>
<url-pattern> / * </ url-pattern>
</ Filter-mapping>
<filter-mapping>
<filter-name> struts-cleanup </ filter-name>
<url-pattern> / * </ url-pattern>
</ Filter-mapping>

Hibernate allows related objects, attributes lazy, lazy, but must ensure that the operation be limited to the same Hibernate Session within range. If the Service Layer Fanhui a delay load feature enabled De Ling Yu object To the Web, Ceng, Dang Web tier Fangwen to Naxie need lazy data, the The loading area of the object in the Hibernate Session Already closure, delay in loading data Daozhi access anomalies. The Spring has provided us with the OpenSessionInViewFilter filters for our well solved the problem. OpenSessionInViewFilter main function is to process each request to bind a Hibernate Session, even if the initial transaction has been completed, you can also load the Web layer delayed the operation. OpenSessionInViewFilter filter Hibernate Session bound to the request thread, it will automatically be Spring's transaction manager to detect. So OpenSessionInViewFilter apply to the use HibernateTransactionManager or JtaTransactionManager Service layer for transaction management environment, can also be used for non-transaction data read-only operation.
request -> open session -> open the connection, begin transaction -> lasting operation -> render (close connection, session) -> response omitted some of the process is not very concerned about.

This article comes from CSDN blog, reproduced, please indicate the source: http://blog.csdn.net/maxracer/archive/2010/03/09/5358711.aspx

分类:Java 时间:2010-05-29 人气:522
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.234 (s). 12 q(s)