【问题标题】:Using OpenSessionInViewInterceptor with Hibernate and JSF 2在 Hibernate 和 JSF 2 中使用 OpenSessionInViewInterceptor
【发布时间】:2011-03-03 07:34:35
【问题描述】:

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

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

请提供详细的答案,因为互联网上到处都是模糊不清、无用的教程。如果提供了分步说明,我也会很高兴提供替代解决方案。

【问题讨论】:

    标签: java hibernate spring jsf interceptor


    【解决方案1】:

    结果证明它非常简单,比拦截器、AOP 和可以在互联网上找到的无数奇怪的东西要简单得多。 将这个 sn-p(请注意我在第二个 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>
    

    【讨论】:

    • 这对我不起作用。也许是因为我使用的是 Hibernate 4,我不知道:S
    猜你喜欢
    • 2012-06-28
    • 1970-01-01
    • 2011-10-14
    • 2011-04-04
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 2012-01-28
    • 2013-01-17
    相关资源
    最近更新 更多