title: How can I replace HibernateTemplate and HibernateCallback in my Spring 4/Hibernate 4 project?
body: <p>I'm working on a very large, old spring3, hibernate3 project, and am tasked with upgrading it to spring4 hibernate4. </p>
  
  <p>Apparently HibernateTemplate is no longer supported in hibernate4, so when I upgrade, our client code breaks. </p>
  
  <p>All of our DAOs extend the HibernateDaoSupport
  Lots of the DAO code looks like this: </p>
  
  <pre><code>public class ManagerDaoImpl extends HibernateDaoSupport implements ManagerDAO {
  
  public List&lt;Person&gt; getManagers() {
      List l = (List) this.getHibernateTemplate().executeFind(new HibernateCallback(){
          public Object doInHibernate(Session session) throws HibernateException, SQLException {
              List result = (List) session.createQuery(MySqlQueryString).list();
              return result;
          }
      });
      return l;
  }
  
  //........
  
  }
  </code></pre>
  
  <p>it is wired up in XML like: </p>
  
  <pre><code>&lt;bean id="managerDaoImpl" class="com.foo.bar.hibernate.ManagerDaoImpl"&gt;
      &lt;property name="sessionFactory" ref="MySessionFactory" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>&lt;bean id="MySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource"     ref="myDataSource" /&gt;
      &lt;property name="configLocation" value="WEB-INF/hibernate.cfg.xml" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p><strong>What I thought I needed to do:</strong></p>
  
  <pre><code>public List&lt;Person&gt; getManagers() {}
              List result = (List) getSessionFactory().getCurrentSession.createQuery(MySqlQueryString).list();
              return result;
      return l;
  }
  </code></pre>
  
  <p>but this gives me an error like:</p>
  
  <pre><code>Property 'sessionFactory' threw exception; nested exception is org.hibernate.HibernateException: No Session found for current thread
  </code></pre>
  
  <p>What am I doing wrong? </p>
  
  <p>I thought the whole point of hibernateTemplate being deprecated is that Spring 3+ could handle the transactions for me? </p>
  
  <p>I really didn't think a "path of least resistances" approach to upgrading such commonly used technologies would involve getting my hands dirty with the likes of sessions sessions and transaction scopes. </p>