开发者社区> 问答> 正文

求帮助! Hibernate 异常org.hibernate.LazyInitializationE

org.hibernate.LazyInitializationException(用hibernate带出从表数据报错),求帮助! Hibernate 异常org.hibernate.LazyInitializationException: could not initialize proxy - no Session:报错

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)

展开
收起
kun坤 2020-06-14 10:04:20 529 0
1 条回答
写回答
取消 提交回答
  • <set> 中设置 lazy="false"
    ######

    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" 

    2020-06-14 10:04:26
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载