title: Spring - SessionFactoryUtils in hibernate 4
body: <p>Considering moving into spring 3.2 with hibernate 4, how should one obtain current session in the context of a transaction? As far as I understand <code>HibernateTransactionManager</code> code (and indeed other TMs) upon beginning a transaction it calls <code>sessionFactory....openSession()</code>, which does NOT set currentSession on hibernate side to this session, but merely returns a new one. Then, the <code>HibernateTransactionManager</code> will maintain the session object using <code>TransactionSynchronizationManager</code> ThreadLocal constructs. Now, if I wanted to access the <code>Session</code> bound to current transaction, I would simply call <code>SessionFactoryUtils.getSession()</code> and that would internally check the <code>TransactionSynchronizationManager</code> for existing session.</p>
  
  <p>Now, in hibernate 4, we do not have that methodology, and we are supposed to use <code>SessionFactory.getCurrentSession()</code> as per HibernateTemplate JavaDoc:</p>
  
  <p><strong>NOTE: As of Hibernate 3.0.1, transactional Hibernate access code can also be coded in plain Hibernate style. Hence, for newly started projects, consider adopting the standard Hibernate3 style of coding data access objects instead, based on SessionFactory.getCurrentSession().</strong></p>
  
  <p>Well, if I am still using <code>HibernateTransactionManager</code>, which uses its OWN session context, and then call <code>SessionFactory.getCurrentSession()</code>, I will not get the current transaction's Session, as I am now referring to hibernate's internal per-thread context, correct? Any pointers would be greatly appreciated</p>
  
  <p>EDIT: Basically I am trying to write my own implementeation of CurrentSessionContext. Previously I would simply use SessionFactoryUtils.getSession(sf, true) and that would return a currently bound session per thread.</p>