title: Hibernate LazyInitializationException: failed to lazily initialize a collection of role
body: <p>I have a web service which is essentially a wrapper for a DAO.  I am calling the web service/DAO to request a collection of entities.  </p>
  
  <p>The "parent" entity class contains a collection of "child" entity objects, i.e. a one-to-many relationship.  The DAO method call which fetches the "parent" entity collection (i.e. myDAO.findAll()) returns with no problem, however the final result of the web service call is this exception:   </p>
  
  <pre><code>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: Station.observations, no session or session was closed
  </code></pre>
  
  <p>Some background information:</p>
  
  <p>I have two entities, Station and Observations.  A Station entity (the "parent") contains a collection of Observations objects (the "children").</p>
  
  <p>The DAO class for Station entities, StationDao has the following code for the method being called, findAll():</p>
  
  <pre><code>private SessionFactory sessionFactory;
  
  private Class&lt;T&gt; persistentClass;
  
  public List&lt;T&gt; findAll()
  {
      return getCurrentSession().createQuery("from " + persistentClass.getName()).list();
  }
  </code></pre>
  
  <p>In this case I'm not interested in the collection of children (observations) related to the parents (stations) being fully fetched before the collection of parents is returned.  However it seems that there is some attempt being made to fully fetch these child (observations) objects before returning the collection of parent (station) objects in the response, and this is failing because the original session is no longer available.</p>
  
  <p>Can anyone suggest how I would get around this error?  Perhaps there's a way to persist the session over the life of the request?</p>
  
  <p>Thanks in advance for your help!</p>
  
  <p>--James</p>