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

    Setting up envers in maven/eclipse

    chrismm

      Hi,

      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 ??

      Thanks,

      Chris Milburn

        • 1. Setting up envers in maven/eclipse
          adamw

          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.


          Adam

          • 2. Setting up envers in maven/eclipse
            chrismm

            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"

                                                     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

            <hibernate-configuration>

            <session-factory name="">

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

                  <property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.181.79:1521:ORARCDQ</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"/>

                

            </session-factory>

            </hibernate-configuration>

             

            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;

             

            @Entity

            @Table( name="ARTICLE")

            @Audited

            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

             

            <dependency>

                <groupId>org.hibernate</groupId>

                <artifactId>hibernate-envers</artifactId>

                <version>3.5.6-Final</version>

                <type>jar</type>

                <scope>compile</scope>

            </dependency>

             

            What have I done wrong ??

            • 3. Setting up envers in maven/eclipse
              adamw

              Did you start your application with these settings?

               

              Adam

              • 4. Setting up envers in maven/eclipse
                chrismm

                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.

                Chris.