title: Open Session in View pattern using Hibernate, Spring, Struts 2
body: <p>I'm working on a project using Hibernate for persisting and Struts 2 for the view pattern.</p>
  <p>My configuration files are:</p>
  <p><strong><code>web.xml</code>:</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
   &lt;web-app&gt;
       //......
       //.....
       &lt;filter&gt;
          &lt;filter-name&gt;struts2&lt;/filter-name&gt;
          &lt;filter-class&gt;
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
          &lt;/filter-class&gt;
        &lt;/filter&gt;
        &lt;filter-mapping&gt;
          &lt;filter-name&gt;struts2&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
        &lt;/filter-mapping&gt;
        
        &lt;!-- The defintion of the root Spring Container shared by all Servlets and Filters --&gt;
        &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt;
        &lt;/context-param&gt;
        &lt;!-- Creates the spring Container shared by all servlet and filters --&gt;
        &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
        &lt;/listener&gt;
        
         &lt;filter&gt; &lt;!-- Get spring to keep the session open for the whole request, so hibernate's lazy loads work --&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;/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;/web-app&gt;
  </code></pre>
  <hr />
  <p><strong><code>applicationContext.xml</code>:</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;beans &gt;
  //.......
  &lt;bean id=&quot;dataSource&quot; class=&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;&gt;
          &lt;property name=&quot;driverClassName&quot; value=&quot;com.mysql.jdbc.Driver&quot;&gt;&lt;/property&gt;
          &lt;property name=&quot;url&quot; value=&quot;jdbc:mysql://localhost:3306/DB_TEST&quot;&gt;&lt;/property&gt;
          &lt;property name=&quot;username&quot; value=&quot;root&quot;&gt;&lt;/property&gt;
          &lt;property name=&quot;password&quot; value=&quot;&quot;&gt;&lt;/property&gt;
      &lt;/bean&gt;
      
      &lt;bean id=&quot;persistenceUnitManager&quot; class=&quot;org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager&quot;&gt;
          &lt;property name=&quot;persistenceXmlLocations&quot;&gt;
              &lt;list&gt;
                  &lt;value&gt;classpath*:META-INF/persistence.xml&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name=&quot;defaultDataSource&quot; ref=&quot;dataSource&quot;&gt;&lt;/property&gt;
      &lt;/bean&gt;
      
      &lt;bean id=&quot;entityManagerFactory&quot; class=&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot;&gt;
          &lt;property name=&quot;persistenceUnitManager&quot; ref=&quot;persistenceUnitManager&quot;&gt;&lt;/property&gt;
      &lt;/bean&gt;
      
      &lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.orm.jpa.JpaTransactionManager&quot;&gt;
          &lt;property name=&quot;entityManagerFactory&quot; ref=&quot;entityManagerFactory&quot;&gt;&lt;/property&gt;
      &lt;/bean&gt;
      
      &lt;tx:annotation-driven transaction-manager=&quot;transactionManager&quot; /&gt;
      &lt;context:annotation-config&gt;&lt;/context:annotation-config&gt;
  &lt;/beans&gt;
  </code></pre>
  <hr />
  <p>My problem is I can't keep Hibernate session open in the view pattern of Struts 2, means when I try to load some data that are not already initialized with Hibernate (like collections for example) i get the <code>org.hibernate.LazyInitializationException</code>, so after doing some research I found that I must add this scope in <code>web.xml </code>to keep the session open in the view pattern.</p>
  <p><strong>Scope:</strong></p>
  <pre><code>&lt;filter&gt; &lt;!-- Get spring to keep the session open for the whole request, so hibernate's lazy loads work --&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;/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;
  </code></pre>
  <hr />
  <p>But even with this I still have the same problem, so can anyone tell me what i'm doing wrong.</p>