我在使用 Spring 3.0.2、hibernate3 和 jsf2 让 OpenSessionInViewFilter 工作时遇到了很大的麻烦。
场景:
有一个 BusinessCaseEntity 包含一些简单的信息属性(字符串和 int 类型)和映射的 EmployeeEntities 列表。此列表映射为
fetch=FetchType.LAZY
首先,我加载所有 BusinessCases 的列表并将它们显示在表格中。为此,我使用了一个 dao 类。显示具有简单属性的商业案例。例如,如果我选择一个特定的商业案例,我想展示相关的员工。因此我只想使用businesscase对象的getter方法——> getEmployees()
以我对 spring 和 hibernate 的简单理解,我知道此时的 BusinessCaseEntity 与后端的任何 spring 和 hibernate(session) 是分离的,并且出现了著名的 LazyLoadingException。
这就是我认为 OpenSessionInViewFilter 到位的地方。我阅读了很多关于如何使用它的说明,但我仍然无法让它在我的应用程序中运行。
在我的 web.xml 中配置了过滤器,我创建了 spring 的 OpenSessionInViewFilter 的一个小子类来做一些输出以用于调试目的。
然而,当涉及到获取所需数据时,过滤器会打开一个新会话,然后抛出 LazyLoadException,然后过滤器关闭会话。为什么会话当前打开时会出现 LazyLoadException?
2011-06-14 19:19:49,734 DEBUG HibernateFilter:239 - Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter
2011-06-14 19:19:49,734 DEBUG HibernateFilter:66 - Opening single Hibernate Session in OpenSessionInViewFilter
Jun 14, 2011 7:19:49 PM com.sun.facelets.FaceletViewHandler handleRenderException
SEVERE: Error Rendering View[/web/caseDetails.xhtml]
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.frivak.cat.db.entities.BusinessCaseEntity.caseClientList, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
LOTS OF STACKTRACE ...
2011-06-14 19:19:49,879 DEBUG HibernateFilter:92 - Closing single Hibernate Session in OpenSessionInViewFilter
我是否以某种方式误解了 OpenSessionInViewFilter 的目的?
我现在很迷茫,非常感谢一些帮助。
谢谢 - 克里斯