7 Replies Latest reply on May 26, 2011 3:39 PM by shyenuganti

    Envers configuration with hibernate.cfg.xml

    shyenuganti

      I am trying to use Envers to audit the updates/inserts into my tables. I have created audit tables with extension _AUDIT in the DB.

       

      But when I actually run the application, I dont see any entries in the audit tables. I even have no errors or exceptions thrown. Entries are being inserted into the main tables but the AUDIT tables are not updated.

       

      Here is my ENVERS configuration :

       

      hibernate.cfg.xml:

       

      <!-- Hibernate ENVERS Configuration -->

      <property name="org.hibernate.envers.audit_table_suffix">_AUDIT</property>

      <property name="org.hibernate.envers.revision_field_name">REVISION_ID</property>

      <property name="org.hibernate.envers.revision_type_field_name">REVTYPE</property>

      <property name="org.hibernate.envers.do_not_audit_optimistic_locking_field">true</property>

      <property name="org.hibernate.envers.default_schema">ROCC</property>

       

      <!-- Hibernate ENVERS Listener Configuration -->

       

      <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"/>

       

       

      My table is as follows:

       

      /**

      * Transaction generated by hbm2java

      */

      @Audited

      @Entity

      @Table(name = "TRANSACTION", schema = "ROCC")

      public class TransactionTable implements java.io.Serializable{...}

       

      The Audit table is TRANSACTION_AUDIT in the same schema.

       

      Can any one tell me why the auditing is not working?

        • 1. Re: Envers configuration with hibernate.cfg.xml
          adamw

          Maybe you can try setting a breakpoint on AuditEventListener to see if the listener is invoked?

           

          Adam

          • 2. Re: Envers configuration with hibernate.cfg.xml
            shyenuganti

            Yes, I tried to build a custom event listener that just does nothing but logs and delegates the call to AuditEventListener. The post-update method is being called. But the audit tables are not being updated !!

            • 3. Re: Envers configuration with hibernate.cfg.xml
              shyenuganti

              Do we need to add "EJB3Post...EventListener"s to the hibernate config file for the Envers persistance to work properly? If Yes, where and how should we add them?

              • 4. Re: Envers configuration with hibernate.cfg.xml
                shyenuganti

                Am I missing any transaction configuration settings in my hibernate.cfg.xml? Which transaction manager should I use for Envers to work?

                • 5. Re: Envers configuration with hibernate.cfg.xml
                  shyenuganti

                  I have added the transaction managers in the config file. This worked.

                   

                  Now I get this error :

                   

                  com.ibm.db2.jcc.c.SqlException: "IMGTEST.REVINFO" is an undefined name.

                   

                  I dont know why it is going for IMGTEST schema when the default schema is defined as ROCC in the cofig file.

                   

                  Is there any other attribute that I have to set  to specify  the schema for REVINFO table?

                  • 6. Re: Envers configuration with hibernate.cfg.xml
                    adamw

                    I think there's a bug with the revision entity not respecting the default schema setting.


                    Try creating a custom revision entity (there's an example in the doc, it can contain just the revision number and revision timestamp fields), and place an @Table annotation with the schema specified on it.

                     

                    And you need transactions as you already found out for Envers to work, that's correct . Luckily you normally want transactions anyway

                     

                    Adam

                    • 7. Re: Envers configuration with hibernate.cfg.xml
                      shyenuganti

                      Thank You Adam ! That solved my issue. I have a created a custom Revision Entity and added it to my hibernate.cfg.xml.

                       

                      I think this Revision Entity bug is happening only when you use the Hibernate Session factory instead of EntityManager. And also if you use hibernate from Spring using HibernateTemplate, we are not seeing this issue.

                       

                      Here I have another question. If I close the session after inserting into the main table, the Envers is not doing the auditing saying THE SESSION IS ALREADY CLOSED. If I leave the session open without closing, the auditing is working.

                       

                      So will ENVERS close the open session after inserting into the audit tables? If not, there willl be number of open sessions that are not closed !!