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

时间:2011-11-04 11:18:02

标签: java hibernate spring jsf-2

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

  

org.hibernate.LazyInitializationException:懒得初始化   角色集合:pts.entity.Invention.inventors,没有   会议或会议已经结束

我的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进行数据库操作。感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您必须确保不在JSF视图之间重定向,例如:

在此视图中,您在vew Filter中的Open 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会话已经关闭。

受此帖的启发: OpenSessionInViewFilter +Redirect in JSF

答案 1 :(得分:0)

什么时候失败?我可以看到你使用Spring Security并在web.xml中定义了springSecurityFilterChain。并且假设过滤器从数据库加载用户,并且用户具有延迟集合,当在OpenSessionInView过滤器开始执行之前,在认证期间创建的Hibernate会话可以关闭时,稍后访问该集合。

请记住,过滤器映射顺序是有意义的,按照过滤器映射的顺序执行过滤器。我会先尝试重新排序。