title: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role (Hibernate + Spring)
body: <p>I have a SpringMVC+Hibernate web application with the following files:</p>
  
  <blockquote>
    <p>applicationContext.xml</p>
  </blockquote>
  
  <pre><code>    &lt;bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"&gt;
      &lt;property name="driverClassName" value="org.postgresql.Driver"/&gt;
      &lt;property name="url" value="jdbc:postgresql://localhost:5432/db"/&gt;
      &lt;property name="username" value="kjhkjkj"/&gt;
      &lt;property name="password" value="dsfa@efe45"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="hibernateSessionFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"&gt;
      &lt;property name="transactionManager" ref="txManager"/&gt;
      &lt;property name="transactionAttributes"&gt;
          &lt;props&gt;
              &lt;prop key="*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
      &lt;property name="target"&gt;
          &lt;bean class="dao.DocumentViewDao"&gt;
              &lt;property name="sessionFactory" ref="hibernateSessionFactory"/&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
      &lt;property name="proxyTargetClass" value="true"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="mappingResources"&gt;
          &lt;list&gt;
      &lt;value&gt;orm/Document.hbm.xml&lt;/value&gt;
      &lt;value&gt;orm/UploadedDocument.hbm.xml&lt;/value&gt;
      &lt;!-- More resource files go here --&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;value&gt;
              hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
              hibernate.show_sql = true
              hibernate.format_sql = true
              hibernate.generate_statistics = true
              hibernate.cache.use_query_cache = true
              hibernate.cache.use_second_level_cache = true
              hibernate.cache.provider_class = org.hibernate.cache.EhCacheProvider
              hibernate.query.substitutions = true 't', false 'f'
          &lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="documentViewDao" class="dao.DocumentViewDao"&gt;
      &lt;property name="sessionFactory" ref="hibernateSessionFactory"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <blockquote>
    <p>dao.DocumentViewDao</p>
  </blockquote>
  
  <pre><code>@Transactional
  public class DocumentViewDao extends HibernateDaoSupport
  {
  public List all ( )
          throws HibernateException
  {
      Session session = this.getSessionFactory ( ).getCurrentSession ( );
  
      return session.createQuery ( new StringBuilder ( 35 )
                      .append ( "SELECT d.id AS id, d.identifier AS identifier, d.title AS title, u.version AS version, u.effectFrom AS effectFrom " )
                      .append ( "FROM Document AS d " )
                      .append ( "LEFT OUTER JOIN d.uploadedDocumentsById AS u " )
                      .append ( "WITH u.isCurrent = true" )
                      .toString ( ) )
              .setCacheable ( true )
              .setResultTransformer( Transformers.ALIAS_TO_ENTITY_MAP )
              .list ( );
  }
  
  public Document getDocument ( int documentId )
          throws HibernateException
  {
      Session session = this.getSessionFactory ( ).getCurrentSession ( );
  
      Document document = null;
  
      Query q = session.createQuery ( "FROM IsoDocument AS d " +
                                      "LEFT OUTER JOIN FETCH d.uploadedDocumentsById " +
                                      "WHERE d.id = :documentId"
      );
      q.setParameter ( "documentId", documentId );
      q.setCacheable ( true );
      document = ( Document ) q.uniqueResult ( );
  
      return document;
  }
  }
  </code></pre>
  
  <blockquote>
    <p>controller.DocumentController</p>
  </blockquote>
  
  <pre><code>public class DocumentController implements Controller
  {   
      public ModelAndView handleRequest ( HttpServletRequest request, HttpServletResponse response )
              throws Exception
      {
          ModelAndView modelAndView = new ModelAndView ( "document" );
  
          WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext ( );
          DocumentViewDao documentViewDao = ( DocumentViewDao ) context.getBean ( "transactionProxy" );
  
          Document document = DocumentViewDao.getDocument ( Integer.parseInt ( request.getParameter ( "id" ) ) );
          modelAndView.addObject ( "document", document );
  
          return modelAndView;
      }
  }
  </code></pre>
  
  <p>The ORM classes and mapping should be OK, I won't write them (lost of lines :))
  So the problem is I get </p>
  
  <blockquote>
    <p>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: orm.Document.uploadedDocumentsById, no session or session was closed</p>
  </blockquote>
  
  <p>when the page generated by controller.DocumentController is refreshed. Obviously there is a problem with the caching and transactions which I cannot solve. Moreover, using all() from dao.DocumentViewDao doen't throw such an exception (but there isn't FETCH in the query). So do you have any ideas? I also will be happy to any comments and/or suggestions on this code in orger to improve it (am new to Spring and Hibernate).</p>