0

I'm having some problems with Spring 3, with annotations and hibernate.

Im trying to execute this in a JSP that will be included in all the others JSPs, its a header.

${pageContext['request'].userPrincipal.principal.notifications}

My User pojo have a relation with notifications like this

    @OneToMany(mappedBy="user", fetch = FetchType.LAZY)
    protected Collection<Notification> notifications;

If I call getNotifications from a Controller or Service correctly annotated, I have no problems, but when i'm trying to execute the code insede a JSP, I received a LazyInitializationException like this one :

org.hibernate.LazyInitializationException: failed to lazily initialize a 
     collection of role: org.prog.para.model.pojo.user.User.notifications, no session or 
     session was closed

I read about the OpenSessionInViewFilter but i don't know why it does not work.

my web.xml :

<filter>
    <filter-name>OpenSessionInViewFilter</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>
</filter>
<filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/app-config.xml
        /WEB-INF/spring/security-config.xml
    </param-value>
</context-param>
<context-param>
    <param-name>defaultHtmlEscape</param-name>
    <param-value>true</param-value>
</context-param>

<servlet>
    <servlet-name>para-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/mvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>para-mvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<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>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/static/*</url-pattern>
</servlet-mapping>

my app-config.xml file is :

<context:component-scan base-package="org.prog.para">
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>

<!-- Datasource -->
<import resource="ds-config.xml" />

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="org.prog.para.model.pojo" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.default_schema">schema</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

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

<tx:annotation-driven transaction-manager="hibernateTransactionManager"
    proxy-target-class="true" mode="proxy" />


<bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="prefix" value="/WEB-INF/views/templates/" />
    <property name="suffix" value=".html" />
    <property name="characterEncoding" value="UTF-8" />
    <property name="order" value="1" />
</bean>

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
    <property name="templateResolvers" ref="emailTemplateResolver" />
</bean>

<bean id="viewResolver" class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine" />
    <property name="characterEncoding" value="UTF-8" />
</bean>


<bean id="springApplicationContext" class="org.prog.para.config.SpringApplicationContext" />

Anyone knows where's my fail? I don't understand why my session is closed when the view is being executed.

I could solve it putting FetchType.EAGER on notifications collection, but it's not what i want. I need it to be a lazy relation.

Thanks in advance !

1 Answers1

0

I think that a problem can be in that you are using both OpenSessionInViewInterceptor and OpenSessionInViewFilter. Try to remove one of them.

EdChum
  • 376,765
  • 198
  • 813
  • 562
  • Sorry, the interceptor was the last solution i tested. Removing the interceptor code still dont work. Also, removing the filter form web.xml and leaving interceptor code, still failing ! – Albert Casanovas Jul 03 '12 at 11:03
  • Can you give more details about ${pageContext['request'].userPrincipal.principal.notifications}. Is it Spring Security authentications? – Alexander Suslov Jul 03 '12 at 11:22
  • ${pageContext['request'].userPrincipal.principal} returns an Object that can be parsed to User (my own User). yes, it is spring security authentication. If I get the principal inside a controller, I can cast it to User and execute correctly getNotifications(). The problem is the OpenSessionInViewFilter don't extends the session to my JSPs – Albert Casanovas Jul 03 '12 at 12:09