4 Replies Latest reply on Mar 7, 2011 6:11 AM by Chris Milburn

    Setting up envers in maven/eclipse

    Chris Milburn Newbie


      Im trying to setup envers for our plain hibernate project, according to the documentation you just have to  add @Audited to your @Entity class and the listeners to your hibernate.cfg.xml file ie :-


           <!-- Envers interceptors, called when an audited table has an update/delete/create called on it. -->


           <event type="post-insert">                     <listener class="org.hibernate.envers.event.AuditEventListener" />     </event>

           <event type="post-update">                   <listener class="org.hibernate.envers.event.AuditEventListener" />     </event>

           <event type="post-delete">                    <listener class="org.hibernate.envers.event.AuditEventListener" />     </event>

           <event type="pre-collection-update">      <listener class="org.hibernate.envers.event.AuditEventListener" />     </event>

           <event type="pre-collection-remove">      <listener class="org.hibernate.envers.event.AuditEventListener" />     </event>

           <event type="post-collection-recreate">   <listener class="org.hibernate.envers.event.AuditEventListener" />     </event>


      according to the Envers Reference Documentation :-


      "And thats' it! You create, modify and delete the entities as always. If you look at the generated schema, you will notice that it is unchanged by adding ........."


      However what do I actually need to do to get the _AUD table generated ??? and what changes to the pom file are needed ??


      Chris Milburn

        • 1. Setting up envers in maven/eclipse
          Adam Warski Master

          If you have hbm2ddl.auto set to update or create, then the tables will be automatically created (just like tables for regular entities).


          If you'd like to generate the schema, there's an Ant task for that: http://docs.jboss.org/hibernate/envers/3.6/reference/en-US/html_single/#schema


          In the pom, you only need to add the dependency on the Envers module.


          • 2. Setting up envers in maven/eclipse
            Chris Milburn Newbie

            Hi Adam,

            I cant get the _AUD tables to be generated ??

            Heres my hibernate.cfg.xml :-


            <?xml version="1.0"?>

            <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"



            <session-factory name="">

                  <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

                  <property name="hibernate.connection.url">jdbc:oracle:thin:@</property>

                  <property name="hibernate.connection.username">rs_dev</property>

                  <property name="hibernate.connection.password">rs_dev</property>

                  <property name="hibernate.connection.pool_size">10</property>

                  <property name="hibernate.hbm2ddl.auto">update</property>

                  <property name="show_sql">true</property>

                  <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

                  <!-- Mapping classes -->

                  <mapping class="com.iontrading.RS.database.Article"/>                <!-- This Class has @Audited set -->

                  <mapping class="com.iontrading.RS.database.Directory"/>

                  <mapping class="com.iontrading.RS.database.Filter"/>

                  <mapping class="com.iontrading.RS.database.Report"/>

                  <mapping class="com.iontrading.RS.database.Template"/>


                 <!-- Envers interceptors, called when an audited table has an update/delete/create called on it. -->


                 <listener class="org.hibernate.envers.event.AuditEventListener" type="post-insert"/>

                   <listener class="org.hibernate.envers.event.AuditEventListener" type="post-update"/>

                 <listener class="org.hibernate.envers.event.AuditEventListener" type="post-delete"/>

                 <listener class="org.hibernate.envers.event.AuditEventListener" type="pre-collection-update"/>

                 <listener class="org.hibernate.envers.event.AuditEventListener" type="pre-collection-remove"/>

                 <listener class="org.hibernate.envers.event.AuditEventListener" type="post-collection-recreate"/>





            heres the Article.java file (elided)


            import javax.persistence.Column;

            import javax.persistence.Entity;

            import javax.persistence.Id;

            import javax.persistence.Table;


            import org.hibernate.envers.Audited;



            @Table( name="ARTICLE")


            public class Article


                @Id    Long id;

                @Column( name="FILTER_ID", nullable= false)    Long filterId;

                @Column( name="TEMPLATE_ID") Long templateId;

                @Column( name="REF_ARTICLE_ID")Long refArticleId;

                @Column( name="TYPE")String type;

                @Column( name="CREATION_DATE")String creationDate;

            ... etc


            and heres the entry in the pom.xml










            What have I done wrong ??

            • 3. Setting up envers in maven/eclipse
              Adam Warski Master

              Did you start your application with these settings?



              • 4. Setting up envers in maven/eclipse
                Chris Milburn Newbie

                Hi Adam, Ive found out this was due to a different hibernate.cfg.xml being picked up further up the classpath.

                Thanks for your help.