OpenSessionInViewFilter не работает в приложении JSF + Spring + Hibernate - PullRequest
1 голос
/ 04 ноября 2011

Я хочу использовать OpenSessionInViewFilter в моем проекте JSF 2.1 + Spring 3.1 + Hibernate 3.6.6 чтобы избавиться от исключения .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>

Мое приложениеContext.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>

Я использую пружинные (HibernateTemplate) hibernate daos для операций с БД. Спасибо за помощь.

Ответы [ 2 ]

1 голос
/ 04 ноября 2011

Вы просто должны быть уверены, что не перенаправляете между JSF-представлениями, например:

В этом представлении ваш Open Session in vew Filter будет работать:измените этот пример следующим образом:

<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 + перенаправление в JSF

0 голосов
/ 08 ноября 2011

Когда не получается? Я вижу, что вы используете Spring Security и у вас есть определенный springSecurityFilterChain web.xml. Предполагается, что фильтр загружает пользователя из базы данных, и у пользователя есть отложенная коллекция, к которой обращаются позже, когда сеанс Hibernate, созданный во время аутентификации, вероятно, закрыт, прежде чем фильтр OpenSessionInView начнет выполняться.

Помните, что порядок отображения фильтров имеет смысл, фильтр, выполняемый в порядке их сопоставлений фильтров, объявляется. Я бы сначала попытался изменить их порядок.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...