title: LazyInitializationException: failed to lazily initialize a collection of role could not initialize proxy - no Session
body: <p>This is asked many times in SO. But they didn't solve my issue so posting again.</p>
  
  <p>Getting following exception while trying to access my application</p>
  
  <pre><code>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.wpt.models.Item.itemCategory, could not initialize proxy - no Session
      at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
      at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214)
      at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
      at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
      at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:294)
      at org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:348)
      at org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:224)
      at org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:155)
      at javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:256)
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;filter&gt;
          &lt;filter-name&gt;HibernateFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.orm.hibernate4.support.OpenSessionInViewFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;sessionFactoryBeanName&lt;/param-name&gt;
              &lt;param-value&gt;hibernate4AnnotatedSessionFactory&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;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;
  </code></pre>
  
  <p>Spring config:</p>
  
  <pre><code>&lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
      p:sessionFactory-ref="hibernate4AnnotatedSessionFactory"&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Item.java</p>
  
  <pre><code>@Entity
  @Table(name="item")
  public class Item {
      @OneToMany(mappedBy="item", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
      private List&lt;ItemCategory&gt; itemCategory = new ArrayList&lt;ItemCategory&gt;();
  }
  </code></pre>
  
  <p>ItemServiceImpl.java</p>
  
  <pre><code>@Override
      @Transactional(readOnly=true)
      public List&lt;Item&gt; getItemsByCategory(int categoryId) {
          return itemDAO.getItemsByCategory(categoryId);
      }
  </code></pre>
  
  <p>In all of the blogs, it's suggested to either go for <code>EAGER</code> fetch, or use <code>OpenSessionInViewFilter</code> with <code>@Transactional</code> support.</p>
  
  <p>It works will if I use <code>EAGER</code> fetch. But not working the other way. I have implemented all service methods with <code>@Transactional</code> annotation. What mistake I am doing here? Can someone please help?</p>