0 Replies Latest reply on Jul 29, 2009 7:46 AM by Marc van Kempen

    HOWTO: Envers with Spring

    Marc van Kempen Newbie


      Since I had some problems getting Envers to work with our Spring project, I though I'd share my notes with you. Additionally I had some "fun" in getting it to work with Oracle. I generated the version schema by hand.


      - Spring 2.5.6
      - Hibernate 3.2.6
      - Envers 1.1.0-GA

      We are using Hibernate directly without using JPA, so the standard configuration does not apply, here is what you need to do:

      in your applicationContext.xml add:

      <!-- Event listener for the Envers auditing framework, see sessionFactory configuration -->
       <bean id="versionsEventListener" class="org.jboss.envers.event.VersionsEventListener" />

      in your sessionFactory bean add:

      <property name="eventListeners">
       <entry key="post-insert" value-ref="versionsEventListener" />
       <entry key="post-update" value-ref="versionsEventListener" />
       <entry key="post-delete" value-ref="versionsEventListener" />
       <entry key="pre-collection-update" value-ref="versionsEventListener" />
       <entry key="pre-collection-remove" value-ref="versionsEventListener" />
       <entry key="post-collection-recreate" value-ref="versionsEventListener" />

      Since Oracle does not like names to begin with an underscore we need to add two more properties to the hibernateProperties of your sessionFactory, see also

      <property name="hibernateProperties">
       <prop key="org.jboss.envers.revisionTypeFieldName">VER_REV_TYPE</prop>
       <prop key="org.jboss.envers.revisionFieldName">VER_REV</prop>

      Finally, we need to rename the default revisions info table, since its name starts with an underscore and Oracle does not like that, this can be achieved by introducing a custom RevisionEntity which extends DefaultRevisionEntity:

       * Oracle doesn't like the default revisions entity table name (_revisions_info, Oracle doesn't allow names to start
       * with '_')
      public class OracleRevisionEntity extends DefaultRevisionEntity {
       // should be empty

      Debugging note: when Envers can't save data in a version table you won't see an exception in your console, however when you look in your hibernate log file (enable DEBUG) you'll see what is going on.