title: Configuring OpenSessionInViewFilter with Spring 3 and Servlet 3
body: <p>i want to configure <code>OpenSessionInViewFilter</code> to able to use hibernate lazy initialization in view, so i added the filter definition in web.xml, but it doesn't work i still get the same lazy initialization exception, here's what i did:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0"&gt; 
  
    &lt;display-name&gt;MyApp&lt;/display-name&gt;
  
    &lt;listener&gt;
      &lt;listener-class&gt;
          org.springframework.web.context.ContextLoaderListener
      &lt;/listener-class&gt;
    &lt;/listener&gt;
  
    &lt;listener&gt;
      &lt;listener-class&gt;
          org.springframework.web.context.request.RequestContextListener
      &lt;/listener-class&gt;
    &lt;/listener&gt;
  
    &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;
          classpath:META-INF/spring/applicationContext.xml
          classpath:META-INF/spring/applicationSecurity.xml
          &lt;/param-value&gt;
  
    &lt;/context-param&gt;
  
    &lt;context-param&gt;
      &lt;param-name&gt;javax.faces.FACELETS_LIBRARIES&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/springsecurity.taglib.xml&lt;/param-value&gt;
    &lt;/context-param&gt;
  
  
    &lt;context-param&gt;
      &lt;param-name&gt;javax.faces.PROJECT_STAGE&lt;/param-name&gt;
      &lt;param-value&gt;Development&lt;/param-value&gt;
    &lt;/context-param&gt;
  
    &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;/&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;
  
    &lt;context-param&gt;
      &lt;description&gt;State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2&lt;/description&gt;
      &lt;param-name&gt;javax.faces.STATE_SAVING_METHOD&lt;/param-name&gt;
      &lt;param-value&gt;server&lt;/param-value&gt;
    &lt;/context-param&gt;
  
    &lt;context-param&gt;
      &lt;param-name&gt;javax.servlet.jsp.jstl.fmt.localizationContext&lt;/param-name&gt;
      &lt;param-value&gt;resources.application&lt;/param-value&gt;
    &lt;/context-param&gt;
  
  
    &lt;listener&gt;
      &lt;listener-class&gt;com.sun.faces.config.ConfigureListener&lt;/listener-class&gt;
    &lt;/listener&gt;
  
    &lt;context-param&gt;
      &lt;param-name&gt;javax.faces.FACELETS_SKIP_COMMENTS&lt;/param-name&gt;
      &lt;param-value&gt;true&lt;/param-value&gt;
    &lt;/context-param&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;dispatcher&gt;FORWARD&lt;/dispatcher&gt;
          &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
          &lt;dispatcher&gt;ERROR&lt;/dispatcher&gt;
      &lt;/filter-mapping&gt;
  
  
      &lt;filter&gt;
        &lt;filter-name&gt;Pretty Filter&lt;/filter-name&gt;
        &lt;filter-class&gt;com.ocpsoft.pretty.PrettyFilter&lt;/filter-class&gt;
        &lt;init-param&gt;
              &lt;param-name&gt;logLevel&lt;/param-name&gt;
              &lt;param-value&gt;ERROR&lt;/param-value&gt;
        &lt;/init-param&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
      &lt;filter-name&gt;Pretty Filter&lt;/filter-name&gt;
        &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
        &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;
        &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
        &lt;dispatcher&gt;ERROR&lt;/dispatcher&gt;
      &lt;/filter-mapping&gt;
  
      &lt;filter&gt;
        &lt;filter-name&gt;hibernateFilter&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;sessionFactoryBeanName&lt;/param-name&gt;
           &lt;param-value&gt;sessionFactory&lt;/param-value&gt;         
        &lt;/init-param&gt;      
     &lt;/filter&gt;
  
     &lt;filter-mapping&gt;
       &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;
       &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
     &lt;/filter-mapping&gt; 
  
  
  
    &lt;servlet&gt;
      &lt;servlet-name&gt;Resource Servlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.icesoft.faces.webapp.CompatResourceServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
  
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Resource Servlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/xmlhttp/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  
    &lt;servlet&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
  
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>applicationContext:</strong></p>
  
  <pre><code>&lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
  
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="packagesToScan" value="${project.groupId}.domain" /&gt;
  
  
          &lt;!-- control the behavior of Hibernate at runtime,All are optional and have reasonable default values --&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;value&gt;
              &lt;!-- hibernate.dialect: allows Hibernate to generate SQL optimized for a particular relational database --&gt;
                  hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
                  hibernate.hbm2ddl.auto=create-drop
                  hibernate.show_sql=true
                  hibernate.format_sql=true
              &lt;/value&gt;
          &lt;/property&gt;
  
      &lt;/bean&gt;
  </code></pre>
  
  <p>please advise why i am <strong>still getting the lazy initialization exception</strong>, thanks in advance.</p>