org.hibernate.LazyInitializationException(用hibernate带出从表数据报错),求帮助!

养生程序猿 发布于 2013/07/20 15:57
阅读 10K+
收藏 0
2013-07-20 15:53:56,297 [ERROR] org.hibernate.LazyInitializationException.<init>(LazyInitializationException.java:19) - failed to lazily initialize a collection of role: com.yu.bill.entity.BillEntity.cargoEntitys, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.yu.bill.entity.BillEntity.cargoEntitys, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:309)
at com.yu.bill.service.BillActionService.getListUserData(BillActionService.java:204)
at com.yu.bill.service.BillActionService.service(BillActionService.java:42)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)
加载中
0
养生程序猿
养生程序猿
<set> 中设置 lazy="false"
0
养生程序猿
养生程序猿

Hibernate 异常org.hibernate.LazyInitializationException: could not initialize proxy - no Session

错误页面提示

could not initialize proxy - no Session

控制台

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

病症:这是一个lazy使用后的Exception,使用迟时加载,在session(hibernate里的session),关闭后使用该对象的未加载变量,也就是说session已经关闭,没有保存到内存中,然后你使用了,导致该异常。


Q:我以get得到一个对象,在JSP页面以EL表达它的实例变量时,也抛出了该异常,在application的测试时却没有出现。
A:推测可能是该实例变量是hibernate进行加载的,可能hibernate本身是以load的方式将该实例变量得到。
解决方法:

方法1.用openSessionInView 
方法2.把这个类的延迟加载禁掉

我自己的代码就多了这么一句话:

         resultExchangeRatePojo.getDollarRmb();//在它的实例变量DollarRmb的session未关闭前存入,实际上将它载入内存。(我就想在没用关闭session之前用一下就可以把值保存到内存中了)

结果:果然页面上就正常显示了。


总结:原因是hibernate的session已经关闭,集合没有被初始化。在hibernate中:hibernate3 默认支持延迟加载(lazy="proxy"我们可以把proxy看作是true),hibernate2 默认立即加载 (lazy="false")。

      在hibernate3中,所有的实体设置文件(user.hbm.xml)中的lazy属性都被默认设成了true,就是当这个类没有被调用时,延时加载,导致了以上情况的发生,在配置文件中将lzay属性设为false就可以了。

----------------------------------------------------------------

原因:
<may-to-one>or<one-to-may>的lazy属性默认为:lazy = "proxy"
解决:<many-to-one>   & <set> 中设置 lazy="false" 

返回顶部
顶部