babb
babb

Reputation: 423

Hibernate Envers with JBoss, Spring and Maven - Doesn't write audit record

I am trying to do a Hibernate Envers implementation in conjuction with Spring, JBoss and Maven.

I have the following:

In the pom.xml I have added the required hibernate-envers:

    <dependency>    
        <groupId>org.hibernate</groupId>    
        <artifactId>hibernate-envers</artifactId>    
        <version>${hibernate.version}</version>
    </dependency>

In my Hibernate persistance class, I am importing the following

import org.hibernate.envers.Audited;

So it looks like

@Entity
@Indexed
@Audited 
@Table(name = "COM_TEST")
public class MyTest extends AbstractReferenceDataObject { ...

I have ensured that there is a COM_TEST_AUD db table with the relevant DB columns as well as a REVINFO db table.

When I run my code, it uses the existing hibernate implementation to update a record in COM_TEST. But nothing is written to the COM_TEST_AUD or REVINFO db tables.

I have the following properties in my persistence.xml

    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
        <property name="hibernate.connection.autocommit" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="validate"/>
        <property name="hibernate.show_sql" value="false"/>
        <property name="hibernate.cache.use_second_level_cache" value="true"/>
        <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/>
        <property name="hibernate.search.default.directory_provider" value="org.hibernate.search.store.FSDirectoryProvider"/>
        <property name="hibernate.search.default.indexBase" value="./lucene/indexes"/>
        <property name="hibernate.search.default.batch.merge_factor" value="10"/>
        <property name="hibernate.search.default.batch.max_buffered_docs" value="10"/>
    </properties>

I have tried just about everything I can think of. I can see when I start JBoss, it does say how it needs and fines the COM_TEST_AUD and REVINFO, so looks like that works.

I just can't work out why it wont write to the DB. There is nothing written to the log file and I have the logging set to:

        <logger category="org.hibernate">
            <level name="DEBUG"/>
        </logger>

I have noticed that in the JBoss modules directory below it uses hibernate-envers-4.0.1.Final.jar:

jboss-as-7.1.1.Final\modules\org\hibernate\envers\main

I have changed my project pom.xml to be 4.0.1.Final instead of 3.5.0-Final, but it didn't make a difference

Can someone please help?

Upvotes: 0

Views: 2275

Answers (1)

Daniel Serodio
Daniel Serodio

Reputation: 4506

Since you're using Hibernate 3.x you need to configure the appropriate EventListeners in persistence.xml, as mentioned in the Quickstart section of the Envers docs:

<property name="hibernate.ejb.event.post-insert"
         value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.post-update"
         value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" />
<property name="hibernate.ejb.event.post-delete"
         value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,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" />

Note: You didn't state that you're using Hibernate via JPA, but since you mention a persistence.xml, you seem to be using JPA. If that's now the case, the configuration is a bit different, as you can see in the documentation.

If you're using Hibernate 4.x, this configuration is not needed

Upvotes: 1

Related Questions