【发布时间】:2011-06-14 17:54:50
【问题描述】:
我在使用 Spring 3.0.2、hibernate3 和 jsf2 使 OpenSessionInViewFilter 正常工作时遇到了很大的麻烦。
场景:
有一个 BusinessCaseEntity 包含一些简单的信息属性(字符串和 int 类型)和一个映射的 EmployeeEntities 列表。此列表与
映射fetch=FetchType.LAZY
首先,我加载所有商务案例的列表并将它们显示在表格中。为此,我使用了一个 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 的用途?
我现在很迷茫,非常感谢一些帮助。
谢谢 - 克里斯
【问题讨论】:
标签: hibernate spring jsf open-session-in-view