title: Spring 3 + LazyInitializationException + OpenSessionInViewFilter
body: <p>I'm having some problems with Spring 3, with annotations and hibernate.</p>
  
  <p>Im trying to execute this in a JSP that will be included in all the others JSPs, its a header.</p>
  
  <pre><code>${pageContext['request'].userPrincipal.principal.notifications}
  </code></pre>
  
  <p>My User pojo have a relation with notifications like this </p>
  
  <pre><code>    @OneToMany(mappedBy="user", fetch = FetchType.LAZY)
      protected Collection&lt;Notification&gt; notifications;
  </code></pre>
  
  <p>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 :</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I read about the OpenSessionInViewFilter but i don't know why it does not work.</p>
  
  <p>my web.xml :</p>
  
  <pre><code>&lt;filter&gt;
      &lt;filter-name&gt;OpenSessionInViewFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;singleSession&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;OpenSessionInViewFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;
          /WEB-INF/spring/app-config.xml
          /WEB-INF/spring/security-config.xml
      &lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;defaultHtmlEscape&lt;/param-name&gt;
      &lt;param-value&gt;true&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;para-mvc&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/mvc-config.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;para-mvc&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;filter&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;filter&gt;
      &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;encoding&lt;/param-name&gt;
          &lt;param-value&gt;UTF-8&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;forceEncoding&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;default&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/static/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p></p>
  
  <p>my app-config.xml file is :</p>
  
  <pre><code>&lt;context:component-scan base-package="org.prog.para"&gt;
      &lt;context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" /&gt;
  &lt;/context:component-scan&gt;
  
  &lt;!-- Datasource --&gt;
  &lt;import resource="ds-config.xml" /&gt;
  
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="packagesToScan" value="org.prog.para.model.pojo" /&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/prop&gt;
              &lt;prop key="hibernate.default_schema"&gt;schema&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="hibernateTransactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven transaction-manager="hibernateTransactionManager"
      proxy-target-class="true" mode="proxy" /&gt;
  
  
  &lt;bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"&gt;
      &lt;property name="prefix" value="/WEB-INF/views/templates/" /&gt;
      &lt;property name="suffix" value=".html" /&gt;
      &lt;property name="characterEncoding" value="UTF-8" /&gt;
      &lt;property name="order" value="1" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine"&gt;
      &lt;property name="templateResolvers" ref="emailTemplateResolver" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="viewResolver" class="org.thymeleaf.spring3.view.ThymeleafViewResolver"&gt;
      &lt;property name="templateEngine" ref="templateEngine" /&gt;
      &lt;property name="characterEncoding" value="UTF-8" /&gt;
  &lt;/bean&gt;
  
  
  &lt;bean id="springApplicationContext" class="org.prog.para.config.SpringApplicationContext" /&gt;
  </code></pre>
  
  <p>Anyone knows where's my fail? I don't understand why my session is closed when the view is being executed.</p>
  
  <p>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.</p>
  
  <p>Thanks in advance !</p>