title: org.hibernate.HibernateException when migrating from Spring 3 to Spring 4
body: <p>I am migrating an application from Spring 3 to Spring 4. In particular, I am migrating it to</p>
  
  <pre><code>    &lt;spring.version&gt;4.2.9.RELEASE&lt;/spring.version&gt;
      &lt;org.springframework.ws.version&gt;3.0.8.RELEASE&lt;/org.springframework.ws.version&gt;       
      &lt;hibernate.version&gt;4.3.11.Final&lt;/hibernate.version&gt;
  </code></pre>
  
  <p>Here is the original code that worked with Spring 3:</p>
  
  <pre><code>public AcmeUserBean loadUserByDn(String userDn) {
      AcmeUserBean result = null;
      Session session = null;
      try {
          session = this.currentSession();
  
          Transaction tx = session.beginTransaction();
  
          Query query = session.createQuery(
              "from AcmeUserBean as user where user.distinguishedName = :userDn");
          query.setString("userDn", userDn);
          List objs = query.list();
  
          if (objs != null &amp;&amp; objs.size() &gt; 0) {
              result = (AcmeUserBean) objs.get(0);
          }
  
          tx.commit();
          session.close();
          return result;
      } finally {
          if (session != null) {
              session.flush();
              session.close();
          }
      }
  
  }
  </code></pre>
  
  <p>With Spring 4, I updated it to:</p>
  
  <pre><code>@Transactional
  public AcmeUserBean loadUserByDn(String userDn) {
      AcmeUserBean result = null;
      Session session = null;
      try
      {
          session = this.getSessionFactory().getCurrentSession();
  
          Query query = session.createQuery(
              "from AcmeUserBean as user where user.distinguishedName = :userDn");
          query.setString("userDn", userDn);
          List objs = query.list();
  
          if (objs != null &amp;&amp; objs.size() &gt; 0) {
              result = (AcmeUserBean) objs.get(0);
          }
  
          return result;
      } finally {
          if (session != null) {
              session.flush();
              session.close();
          }
      }
  }
  </code></pre>
  
  <p>Each line in the method succeeds and <code>result</code> is not null at the return line.  However, an exception occurs when the method exits:</p>
  
  <blockquote>
    <p>org.springframework.transaction.TransactionSystemException: Could not
    commit Hibernate transaction; nested exception is
    org.hibernate.TransactionException: commit failed</p>
  </blockquote>
  
  <p>The containing class is a 'Dao' class and is annotated as @Repository.</p>
  
  <p>First of all, is my migration of the code correct?  Second, what is causing the exception and how can I fix it?</p>