title: Hibernate+Spring SessionFactory configuration
body: <p>What is the right way to configure SessionFactory?</p>
  
  <p>If I do it this way:</p>
  
  <pre><code>&lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
               p:dataSource-ref="dataSource"
               p:packagesToScan="ua.com.javer.flowerexpert"/&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"
              p:sessionFactory-ref="sessionFactory" /&gt;
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>nested exception is org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
  </code></pre>
  
  <p>And if I change to <code>AnnotationSessionFactoryBean</code>:</p>
  
  <pre><code>    &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
               p:dataSource-ref="dataSource"
               p:packagesToScan="ua.com.javer.flowerexpert"/&gt;
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>nested exception is java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
  </code></pre>
  
  <p>Even though in some older project <code>hibernate3.annotation.AnnotationSessionFactoryBean</code> works fine.</p>
  
  <p>My <code>pom.xml</code> contains:</p>
  
  <pre><code>        &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
          &lt;version&gt;5.0.1.Final&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
                  &lt;artifactId&gt;hibernate-jpa-2.1-api&lt;/artifactId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;version&gt;5.0.1.Final&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;c3p0&lt;/groupId&gt;
          &lt;artifactId&gt;c3p0&lt;/artifactId&gt;
          &lt;version&gt;0.9.1.2&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>Here's my <strong>Service</strong> class:</p>
  
  <pre><code>@Service("colorsService")
  @Transactional
  public class ColorsService {
  @Autowired
  private ColorDao colorDao;
  
  public List&lt;Color&gt; getAllColors() {
      return colorDao.getAllColors();
  }
  }
  </code></pre>
  
  <p>And here's the <strong>DAO</strong>:</p>
  
  <pre><code>@Component
  @Repository("colorDao")
  public class ColorDaoHibernate implements ColorDao {
  
  @Autowired
  private SessionFactory sessionFactory;
  
  public ColorDaoHibernate() {
  }
  
  @Override
  public List&lt;Color&gt; getAllColors() {
      Session session = sessionFactory.getCurrentSession();
  // StatelessSession session = sessionFactory.openStatelessSession();
      Query query = session.createQuery("FROM Color");
      return  query.list();
  }
  }
  </code></pre>
  
  <p><strong>NOTICE:</strong></p>
  
  <p>If I use <code>sessionFactory.openStatelessSession();</code> in DAO class <code>hibernate5.LocalSessionFactoryBean</code> in session configuration would not cause a problem. </p>
  
  <p>But the point is - I want to use <code>sessionFactory.getCurrentSession();</code>
  How can I achieve this?</p>