java“未能延迟初始化角色集合”异常即使在我使用OSIV模式时也会发生
我使用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
更奇怪的是,异常日志只存在于日志文件中,浏览网站没有问题
这是怎么发生的
# 1 楼答案
这通常发生在处理已解除附着的对象时。您必须将对象重新连接到会话,以便Hibernate可以使用它
使用
session.merge()
或session.refresh()
这里还有一些例子:What is the proper way to re-attach detached objects in Hibernate?
# 2 楼答案
在我的例子中,这是因为缓存的对象。由于缓存对象被分离
LazyInitializationException
在访问某些属性时发生,这些属性稍后尚未初始化