将 OpenSessionInViewInterceptor 与 Hibernate 和 JSF 2 结合使用

发布于 2024-09-05 11:32:50 字数 594 浏览 8 评论 0原文

我正在 Hibernate、Spring 和 JSF2 中仅使用注释构建应用程序。如何利用 Spring 中的 OpenSessionInViewInterceptor 来捕获可能在 bean 中打开的任何休眠会话?

我试图优雅地解决常见的“未能延迟初始化角色集合:your.Class.assocation 没有会话或会话已关闭”。尝试从另一个 POJO 中尚未初始化的 POJO 列表(由包含我要读取的项目对象列表的 DAO 检索的标签实体)读取时出现问题。我发现了这个:
http://www.paulcodding .com/blog/2008/01/21/using-the-opensessioninviewinterceptor-for-spring-hibernate3/
但未能在我的环境中使用它。

请提供详细的答案,因为互联网上充满了模糊的、无用的教程。如果提供了分步说明,我也将非常感谢替代解决方案。

I'm building an application in Hibernate, Spring and JSF2 using only annotations. How can I take advantage of OpenSessionInViewInterceptor found in Spring to catch any hibernate session that might open within a bean?

I'm trying to elegantly solve the common “failed to lazily initialize a collection of role: your.Class.assocation no session or session was closed.” problem when trying to read from a yet uninitialized list of POJOs inside another POJO (A Tag entity retrieved by a DAO that contains a List of Project objects I want to read). I've found this:
http://www.paulcodding.com/blog/2008/01/21/using-the-opensessioninviewinterceptor-for-spring-hibernate3/
but failed to make use of it in my environment.

Please provide a detailed answer, as the Internet is full of foggy, unhelpful tutorials. I'll also be greatful for an alternative solution, given a step-by-step instruction is provided.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汹涌人海 2024-09-12 11:32:50

事实证明它非常简单,比拦截器、AOP 和互联网上可以找到的无数奇怪的东西简单得多。
将此片段(注意第二个 init-param 中的小注释)插入到您的 web.xml 中,以忘记所有的烦恼。

<!-- Hibernate OpenSession Filter -->
<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>singleSession</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>***WhateverTheNameOfYourSessionFactoryBeanIs***</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

It turned out to be quite simple, much simpler than interceptors, AOP and the myriads of weirdness that can be found all over the internet.
Insert this snippet (mind my little comment in the second init-param) into your web.xml to forget all your woes.

<!-- Hibernate OpenSession Filter -->
<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>singleSession</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>***WhateverTheNameOfYourSessionFactoryBeanIs***</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文