recent experiences integrating Envers
jonhillmcltd Mar 25, 2009 7:03 AMHi All
I've just spent the last couple of days implementing Envers into our code-base and thought it may be useful to share some of the experience of getting things setup in our environment.
Our project is built using Maven and uses the traditional Hibernate setup without annotations. Our unit and integration tests use Unitils http://www.unitils.org.
Unitils has a dependency on Hibernate-3.2.0.ga and Spring-hibernate3-2.02, so we had been using Hibernate 3.2.0.ga in production too.
I had some difficulties trying to work out which versions of Envers and Hibernate to use and how to configure using hibernate.cfg.xml and my Maven pom still makes me nervous. I opted to configure for Envers 1.2.0.GA and below are some of the configurations and things I had to do.
Extract from Maven pom.xml with comments. As you can see, it has not been too straight-forward. I'm still depending on Hibernate 3.3.0.CR2. If anyone can help me improve on this pom it would be much appreciated.
<!-- needed by Unitils to run Hibernate tests --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-hibernate3</artifactId> <version>2.0.8</version> <scope>test</scope> <!-- 2.0.8 uses Hibernate 3.2.5 which is not compaitble with all the event types needed for envers --> <exclusions> <exclusion> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> </exclusion> </exclusions> </dependency> <!-- Version GA or CR1 OR SP1 OR 3.3.1 throws NoSuchMethodError: org.hibernate.event.PreInsertEvent.getSource() something quite wrong here see http://opensource.atlassian.com/projects/hibernate/browse/HV-66 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.0.CR2</version> <!-- Excluded because this version clashes with the version requried for Envers http://forum.hibernate.org/viewtopic.php?p=2401380 --> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.8.0.GA</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.4.0.GA</version> <exclusions> <exclusion> <!-- has a ref. to a different core --> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.4.0.GA</version> </dependency> <!-- Version 3.1.0.GA throws NoSuchMethodError: org.hibernate.event.PreInsertEvent.getSource() --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>3.0.0.GA</version> </dependency> <!-- Need to configure Jboss repo in local settings.xml --> <dependency> <groupId>org.jboss.envers</groupId> <artifactId>jboss-envers</artifactId> <version>1.2.0.GA-hibernate-3.3</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.5.6</version> </dependency>
I have had to update the Hibernate Configuration DTD that Eclipse references in order for Eclipse to recognise the collection event types in hibernate.cfg.xml.
<event type="post-insert"> <listener class="org.hibernate.envers.event.AuditEventListener" /> </event> <event type="post-delete"> <listener class="org.hibernate.envers.event.AuditEventListener" /> </event> <!-- needed to update the Eclipse Hibernate 3.0 dtd in order to recognise these collection events --> <event type="pre-collection-update"> <listener class="org.hibernate.envers.event.AuditEventListener" /> </event> <event type="post-collection-recreate"> <listener class="org.hibernate.envers.event.AuditEventListener" /> </event> <event type="pre-collection-remove"> <listener class="org.hibernate.envers.event.AuditEventListener" /> </event>
And finally I have created a RevisionEntity Class so that soon we can log extra information with each revision.
@org.hibernate.envers.RevisionEntity public class RevisionEntity extends DefaultRevisionEntity { }
All tests are running nicely with 'mvn test' and the schema gets generated with the additional audit tables. I am looking forward to start tackling the use-cases that Envers should help us with and I hope to write something up about that soon. My pom.xml needs another look and I could do with getting 'mvn hibernate3:hbm2ddl' to generate the additional tables too at somepoint.
Jon Hill
http://www.mediacells.com