【问题标题】:"failed to lazily initialize a collection of role" exception occurs even when I use OSIV pattern“未能延迟初始化角色集合”即使我使用 OSIV 模式也会发生异常
【发布时间】:2012-03-30 14:48:03
【问题描述】:

我使用OpenSessionInViewInterceptor 来避免 LazyInitializationException。

它运行良好,但有时即使我使用的是 OSIV,也会发生 LazyInitializationException。例外是这样的:

SEVERE: Servlet.service() for servlet [appServlet] in context with path [] 
    threw exception [Request processing failed; nested exception is 
    org.apache.tiles.impl.CannotRenderException: ServletException including path 
    '/WEB-INF/views/home/master.jsp'.] with root cause org.hibernate.LazyInitializationException: 
    failed to lazily initialize a collection of role: com..., 
    no session or session was closed 
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersisten
tCollection.java:383)
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(Abs
tractPersistentCollection.java:375)
        at 

更奇怪的是异常日志只在日志文件中,浏览网站没有问题。

这是怎么回事?

【问题讨论】:

  • 您是否在您的应用程序中进行线程处理?您能否显示设置 OpenSessionInViewInterceptor 的 applicationContext.xml/web.xml 的相关部分?查看导致抛出异常的代码区域也可能会有所帮助。

标签: java hibernate transactions lazy-loading open-session-in-view


【解决方案1】:

这通常发生在您使用分离的对象时。您必须将对象重新附加到会话,以便 Hibernate 可以使用它。

使用session.merge() 或session.refresh()

这里你也有一些例子:What is the proper way to re-attach detached objects in Hibernate?

【讨论】:

    【解决方案2】:

    就我而言,这是因为缓存对象。由于缓存对象被分离LazyInitializationException 发生在访问某些尚未初始化的属性时,稍后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-03
      • 2019-08-06
      • 2017-08-16
      • 1970-01-01
      • 2011-04-27
      • 2013-12-08
      相关资源
      最近更新 更多