title: Initialization of collection throws LazyInitializationException
body: <p>I have a problem with the access to a collection from <code>JSP</code> file. Application is based on MVC Spring framework. I put <code>OpenSessionInViewFilter</code> filter to my <code>web.xml</code>. When I want to access to URL with mentioned file, it throws me </p>
  
  <p><code>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.app.cloud.hibernate.Product.availabilities, no session or session was closed</code></p>
  
  <p>I use domain driven design in my app. When I changed fetch type in class <code>Product</code> from <code>FetchType.LAZY</code> to <code>FetchType.EAGER</code>, it worked.</p>
  
  <p><em>stacktrace</em></p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [spring] in context with path [] threw exception [org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.app.cloud.hibernate.Product.availabilities, no session or session was closed] with root cause
  org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.app.cloud.hibernate.Product.availabilities, no session or session was closed
      at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
      at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
      at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:119)
      at org.hibernate.collection.PersistentSet.isEmpty(PersistentSet.java:169)
      at org.apache.el.parser.AstEmpty.getValue(AstEmpty.java:55)
      at org.apache.el.parser.AstNot.getValue(AstNot.java:44)
      at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
      at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
      at org.apache.jsp.WEB_002dINF.jsp.product.products_jsp._jspx_meth_c_005fwhen_005f0(products_jsp.java:321)
      at org.apache.jsp.WEB_002dINF.jsp.product.products_jsp._jspx_meth_c_005fchoose_005f0(products_jsp.java:290)
      at org.apache.jsp.WEB_002dINF.jsp.product.products_jsp._jspx_meth_c_005fforEach_005f0(products_jsp.java:233)
      at org.apache.jsp.WEB_002dINF.jsp.product.products_jsp._jspx_meth_c_005fif_005f0(products_jsp.java:185)
      at org.apache.jsp.WEB_002dINF.jsp.product.products_jsp._jspService(products_jsp.java:141)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
      at org.apache.jasper.servlet.JspServlet._serviceJspFile(JspServlet.java:390)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
      at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
      at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
      at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
      at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
      at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
      at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
      at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:123)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:581)
      at org.apache.catalina.core.StandardHostValve.__invoke(StandardHostValve.java:171)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1686)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p><em>web.xml</em></p>
  
  <pre><code>  &lt;welcome-file-list&gt;
        &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;
  
    &lt;servlet&gt;
        &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
        &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&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;spring&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  
  
  &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;sessionFactoryBeanName&lt;/param-name&gt;
      &lt;param-value&gt;sessionFactory&lt;/param-value&gt;
    &lt;/init-param&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;!-- Context parameters --&gt;
    &lt;context-param&gt;
      &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;WEB-INF/log4j.properties&lt;/param-value&gt;
    &lt;/context-param&gt;
    &lt;context-param&gt;
      &lt;param-name&gt;log4jExposeWebAppRoot&lt;/param-name&gt;
      &lt;param-value&gt;false&lt;/param-value&gt;
    &lt;/context-param&gt;
    &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt;
    &lt;/listener&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p><em>spring-servlet.xml</em></p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc" 
  xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
  xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
      http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt;
  
  &lt;mvc:annotation-driven /&gt;
  &lt;tx:annotation-driven /&gt;
  &lt;context:component-scan base-package="com.app.cloud" /&gt;
  
  &lt;bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"&gt;
      &lt;property name="basename" value="spring-views" /&gt;
      &lt;property name="order" value="1" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jspViewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="viewClass"
          value="org.springframework.web.servlet.view.JstlView" /&gt;
      &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt;
      &lt;property name="suffix" value=".jsp" /&gt;
      &lt;property name="order" value="2" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- JDBC --&gt;
  &lt;bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/jdbc.properties" /&gt;
  
  &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
      p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /&gt;
  
  &lt;!-- Hibernate --&gt;
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="configLocation"&gt;
          &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="configurationClass"&gt;
          &lt;value&gt;org.hibernate.cfg.AnnotationConfiguration&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${jdbc.dialect}&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="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p><em>ProductService.java</em></p>
  
  <pre><code>    @Transactional
      public interface ProductService {
  
          @Transactional
          public void add(Product product);
          @Transactional
          public List&lt;Product&gt; getAll();
          @Transactional
          public Product get(Integer idProduct);
          @Transactional
          public void remove(Integer id);
          @Transactional
          public void edit(Product product);
  
      }
  </code></pre>
  
  <p><em>ProductServiceImpl.java</em></p>
  
  <pre><code>@Service
  @Transactional
  public class ProductServiceImpl implements ProductService {
  
      @Autowired
      private ProductDAO productDAO;
  
      @Transactional
      public void add(Product product) {
          productDAO.add(product);
      }
  
      @Transactional
      public List&lt;Product&gt; getAll() {
          return productDAO.getAll();
      }
  
      @Transactional
      public void remove(Integer id) {
          productDAO.remove(id);
      }
  
      @Transactional
      public Product get(Integer idProduct) {
          return productDAO.get(idProduct);
      }
  
      @Transactional
      public void edit(Product product) {
          productDAO.edit(product);
      }
  
  
  }
  </code></pre>
  
  <p><em>ProductDAO.java</em></p>
  
  <pre><code>    public interface ProductDAO {
  
          public void add(Product product);
          public void edit(Product product);
          public List&lt;Product&gt; getAll();
          public Product get(Integer idProduct);
          public void remove(Integer id);
  
      }
  </code></pre>
  
  <p><em>ProductDAOImpl.java</em></p>
  
  <pre><code>@Repository
  public class ProductDAOImpl implements ProductDAO {
  
      @Autowired
      private SessionFactory sessionFactory;
  
      public void add(Product product) {
          sessionFactory.getCurrentSession().save(product);
      }
  
      public List&lt;Product&gt; getAll() {
          return sessionFactory.getCurrentSession().createQuery("from Product")
                  .list();
      }
  
      public void remove(Integer id) {
          Product product = (Product) sessionFactory.getCurrentSession().load(
                  Product.class, id);
          if (null != product) {
              sessionFactory.getCurrentSession().delete(product);
          }
      }
  
      public Product get(Integer idProduct) {
          Product product = (Product) sessionFactory.openSession().get(
                  Product.class, idProduct);
          return product;
      }
  
      @Override
      public void edit(Product product) {
          sessionFactory.getCurrentSession().update(product);
      }
  
  }
  </code></pre>
  
  <p><em>Product.java</em></p>
  
  <pre><code>@Entity
  @Table(name = "product", catalog = "app")
  public class Product implements java.io.Serializable {
  
      private Integer idProduct;
  
      @NotEmpty
      @Length (min=1,max=70)
      private String name;
  
      private Set&lt;Availability&gt; availabilities = new HashSet&lt;Availability&gt;(0);
  
      public Product() {
      }
  
      public Product(String name) {
          this.category = category;
          this.name = name;
          this.lastUpdate = lastUpdate;
          this.actionFlag = actionFlag;
      }
  
      public Product(String name,
              Set&lt;Availability&gt; availabilities,
              ) {
          this.name = name;
          this.availabilities = availabilities;
      }
  
      @Id
      @GeneratedValue(strategy = IDENTITY)
      @Column(name = "id_product", unique = true, nullable = false)
      public Integer getIdProduct() {
          return this.idProduct;
      }
  
      public void setIdProduct(Integer idProduct) {
          this.idProduct = idProduct;
      }
  
      @Column(name = "name", nullable = false, length = 70)
      public String getName() {
          return this.name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      @OneToMany(fetch = FetchType.LAZY, mappedBy = "product")
      public Set&lt;Availability&gt; getAvailabilities() {
          return this.availabilities;
      }
  
      public void setAvailabilities(Set&lt;Availability&gt; availabilities) {
          this.availabilities = availabilities;
      }
  
  }
  </code></pre>