title: Does HibernateTemplate work with Envers? If so, how?
body: <p>I am trying to use Envers on a project that also uses Hibernate and Spring - and I appreciate a lot the code reduction offered by HibernateTemplate.</p>
  
  <p>I configured Envers under JPA, and after a few tweaks I was able to have the schema generated by the EnversHibernateToolTask Ant task (including the auditing tables). However, when I write code such as:</p>
  
  <pre><code>    hibernateTemplate.saveOrUpdate(f);
  </code></pre>
  
  <p>the data is persisted, but nothing goes to the auditing tables. Conversely, if I write:</p>
  
  <pre><code>    EntityManager em = emf.createEntityManager();
      em.getTransaction().begin();
      em.persist(f);
      em.getTransaction().commit();
  </code></pre>
  
  <p>then data goest to the audit tables (but I'd rather use the former syntax - I know using JPA's EntityManager decouples that code from Hibernate, but it simple does not pay off the hassle - changing ORM engine is not in my wildest dreams for this project.)</p>
  
  <p>It may help to check my <code>applicationContext.xml</code> configuration:</p>
  
  <pre><code>&lt;bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="persistenceUnitName" value="projetox" /&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="showSql" value="true" /&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="packagesToScan" value="com.w2it.projetox.model" /&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close"&gt;
      ...
  &lt;/bean&gt;
  </code></pre>
  
  <p>and here is my <code>persistence.xml</code> setup:</p>
  
  <pre><code>&lt;persistence-unit name="projetox" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;jta-data-source&gt;java:/DefaultDS&lt;/jta-data-source&gt;
      &lt;properties&gt;
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /&gt;
          &lt;!--  Hibernate Envers --&gt;
          &lt;property name="hibernate.ejb.event.post-insert"
              value="org.hibernate.envers.event.AuditEventListener" /&gt;
          &lt;property name="hibernate.ejb.event.post-update"
              value="org.hibernate.envers.event.AuditEventListener" /&gt;
          &lt;property name="hibernate.ejb.event.post-delete"
              value="org.hibernate.envers.event.AuditEventListener" /&gt;
          &lt;property name="hibernate.ejb.event.pre-collection-update"
              value="org.hibernate.envers.event.AuditEventListener" /&gt;
          &lt;property name="hibernate.ejb.event.pre-collection-remove"
              value="org.hibernate.envers.event.AuditEventListener" /&gt;
          &lt;property name="hibernate.ejb.event.post-collection-recreate"
              value="org.hibernate.envers.event.AuditEventListener" /&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p>Does anyone have a hint on what is going on here? Thank you!</p>