Does HibernateTemplate work with Envers? If so, how?

I am trying to use Envers in a project that also uses Hibernate and Spring, and I really appreciate the code reduction offered by HibernateTemplate.

I configured Envers under JPA, and after several settings, I was able to get the schema generated using the EnversHibernateToolTask ​​Ant task (including audit tables). However, when I write code, for example:

    hibernateTemplate.saveOrUpdate(f);

data is saved, but nothing happens in the audit tables. And vice versa, if I write:

    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.persist(f);
    em.getTransaction().commit();

then the data falls into the audit tables (but I would prefer to use the previous syntax - I know that using JPA EntityManager separates this code from Hibernate, but it just does not pay for the ORM engine, which changes the dreams for this project.)

This may help verify the configuration applicationContext.xml:

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="projetox" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
        </bean>
    </property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.w2it.projetox.model" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        </props>
    </property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    ...
</bean>

persistence.xml:

<persistence-unit name="projetox" transaction-type="RESOURCE_LOCAL">
    <jta-data-source>java:/DefaultDS</jta-data-source>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
        <!--  Hibernate Envers -->
        <property name="hibernate.ejb.event.post-insert"
            value="org.hibernate.envers.event.AuditEventListener" />
        <property name="hibernate.ejb.event.post-update"
            value="org.hibernate.envers.event.AuditEventListener" />
        <property name="hibernate.ejb.event.post-delete"
            value="org.hibernate.envers.event.AuditEventListener" />
        <property name="hibernate.ejb.event.pre-collection-update"
            value="org.hibernate.envers.event.AuditEventListener" />
        <property name="hibernate.ejb.event.pre-collection-remove"
            value="org.hibernate.envers.event.AuditEventListener" />
        <property name="hibernate.ejb.event.post-collection-recreate"
            value="org.hibernate.envers.event.AuditEventListener" />
    </properties>
</persistence-unit>

, ? !

+2
2

HibernateTemplate JPA-, JpaTemplate, .

, Envers HibernateTemplate, , JPA ( , persistence.xml ), EntityManager. Hibernate, HibernateTemplate, .

+2

, , @Transactional Dao , dao.save()/update.

eventlistener, , transcational Spring FW. Spring , .

+1

All Articles