title: Advantages of using Hibernate Callback?
body: <p>I am not able to understand advantages of using Hibernate Callback method, are there any advantages or specific use case where we should go for it. </p>
  
  <pre><code>public List findRecentRequests(final int offset, final int length)
  {
      List list = getHibernateTemplate().executeFind(new HibernateCallback()
      {
          public Object doInHibernate(Session session) throws HibernateException
          {
              Query q = session.createQuery(FIND_RECENT_REQUESTS);
              q.setFirstResult(offset);
              q.setMaxResults(length);
              return q.list();
          }
      });
      return list;
  }
  </code></pre>
  
  <p>Also one more important question is that does HibernateCallback method close session everytime after query returns values? I have use case where am calling this function multiple times on every refresh of status page and so will it everytime open session and query database or will it store query results in memory and then everytime I make call to this function, results would be popped out from memory. </p>
  
  <p>I have read(<a href="http://java-x.blogspot.com/2008/04/integrating-spring-and-hibernate.html">Reference</a>):</p>
  
  <p>The spring HibernateTemplate.execute() by default closes any open sessions upon completion. When used with lazy initialization you may get a LazyInitializationException like the following</p>
  
  <blockquote>
    <p>org.hibernate.LazyInitializationException: could not initialize proxy - no Session</p>
  </blockquote>
  
  <p>Any reference to relevant documentation part would be highly appreciated. </p>
  
  <p><strong>Update:</strong></p>
  
  <p>In my case am using ejb transactions and setting it to "support" and i believe in that case as transaction is set to support, it's optional and so everytime new session will be created and hibernate will query database to get results and so that's were am having bottleneck, will that an right assumptions to make?</p>