3 Replies Latest reply on Mar 29, 2009 7:14 PM by Domenico Gambelli

    recent experiences integrating Envers

    Jon Hill Newbie

      Hi 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

        • 1. Re: recent experiences integrating Envers
          Adam Warski Master

          Hello,

          looking forward to your further experiences :) Hope everything will work fine. One question though: why can't you use Hibernate 3.3.1, but use 3.3.0.CR2?

          Such guides are always helpful and if you have any more hints maybe add them to the FAQ:
          https://www.jboss.org/community/docs/DOC-13216

          Adam

          • 2. Re: recent experiences integrating Envers
            Jon Hill Newbie

            Hi Adam

            If I use Hibernate 3.3.1.GA (or 3.3.0 GA|SP1) I get a NoSuchMethodError.
            There is a reference to something similar here http://opensource.atlassian.com/projects/hibernate/browse/HV-66. I must have some combination of libraries incorrect I guess.

            Jon


            Caused by: java.lang.NoSuchMethodError: org.hibernate.event.PreInsertEvent.getSource()Lorg/hibernate/engine/SessionImplementor;
            at org.hibernate.validator.event.ValidateEventListener.onPreInsert(ValidateEventListener.java:172)
            at org.hibernate.action.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:142)
            at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:65)
            at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
            at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
            at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
            at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
            at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:154)
            at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:110)
            at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
            at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:645)
            at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:619)
            at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:623)
            ... 10 more

            • 3. Re: recent experiences integrating Envers
              Domenico Gambelli Newbie

              Hi,
              I have a similar problem:
              I don't know wihch libraries combination to use envers.
              We have these component:
              Jboss 4.2.3, with hibernate inside (That is 3.2.4sp1)
              Spring 2.5
              Envers 1.2.0 GA

              Somebosy Knows which jar includes to play with this game?

              Thanks