java - OpenSessionInViewFilter 在 JSF+Spring+Hibernate 应用程序中不起作用

标签 java hibernate spring jsf-2

我想在我的 JSF 2.1+Spring 3.1+Hibernate 3.6.6 项目中使用 OpenSessionInViewFilter 为了摆脱.LazyInitializationException。 虽然我在 web.xml 中声明了过滤器,但我不断收到;

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: pts.entity.Invention.inventors, no session or session was closed

我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.tt</url-pattern>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>excite-bike</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    </listener>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>  

</web-app>

我的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <import resource="classpath:applicationContext-security.xml" />   

    <context:annotation-config />
    <context:component-scan base-package="pts.component" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
                <prop key="hibernate.show_sql">false</prop>
                 <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <value>pts.entity</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
        <property name="url" value="jdbc:mysql://localhost/PTS" /> 
        <property name="username" value="root" /> <property name="password" 
            value="" /> 
    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"
        name="template">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>


    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="transactionTemplate"
        class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="txManager" />
    </bean>

    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="view">
                    <bean class="pts.scope.ViewScope" />
                </entry>
            </map>
        </property>
    </bean>

</beans>

我使用 spring Managed(HibernateTemplate) hibernate daos 进行数据库操作。感谢您的帮助。

最佳答案

您只需确保没有在 JSF View 之间重定向,例如:

在此 View 中,您在 vew Filter 中的“打开 session ”将起作用:

<navigation-rule>
    <from-view-id>start.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>page1</from-outcome>
        <to-view-id>page1.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

如果您这样修改此示例:

<navigation-rule>
    <from-view-id>start.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>page1</from-outcome>
        <to-view-id>page1.xhtml</to-view-id>
        <redirect />
    </navigation-case>
</navigation-rule>

您的过滤器不会帮助您,因为您正在重定向,并且这是全新的请求,并且您的原始 Hibernate session 已关闭。

受到这篇文章的启发: OpenSessionInViewFilter +Redirect in JSF

关于java - OpenSessionInViewFilter 在 JSF+Spring+Hibernate 应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8008437/

相关文章:

java - 您如何将以下三元运算符转换为常规 if 语句?

java - QueryDSL查询异常

java - HQL : Named Parameter not found even if it exist

java - Map 中的 Spring 绑定(bind)值

java - @PostConstruct 和 "No Hibernate Session bound to thread"异常

java - 使我的单选按钮在 Android 中成为选中状态

java - 相当于 Oracle DB SQL 中的 Number(38)?

java - Hibernate EnVers 中的@NotAudited 和 RelationTargetAuditMode.NOT_AUDITED 有什么区别?

java - Spring - 将 bean 定义从 XML 更改为注释

java - 当我通过 CLI 发出 Maven 命令时,如何使用 Maven 调试 Intellij 2018.3.2