5 Replies Latest reply on Nov 24, 2010 10:35 AM by fmassart

    Custom revision entity problem

    konyshev

      Hi there!

       

      I'm trying to setup audit for our project.

      I started from the default configuration which works fine.

       

      The next step is to store the user which has made changes.

      Following by the manual I created custom entity revision:

       

       

      package ru.csbi.registry.utils.audit;
      import org.hibernate.envers.RevisionNumber;
      import org.hibernate.envers.RevisionTimestamp;
      import org.hibernate.envers.RevisionEntity;
      import javax.persistence.Id;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Entity;
      import javax.persistence.Table;
      @Entity
      @Table(name="REVISIONS")
      @RevisionEntity(CustomRevisionListener.class)
      public class CustomRevisionEntity {
          @Id
          @GeneratedValue
          @RevisionNumber
          private int id;
          @RevisionTimestamp
          private long timestamp;
          private String username;
      public int getId() {
      return id;
      }
      public void setId(int id) {
      this.id = id;
      }
      public long getTimestamp() {
      return timestamp;
      }
      public void setTimestamp(long timestamp) {
      this.timestamp = timestamp;
      }
      public String getUsername() {
      return username;
      }
      public void setUsername(String username) {
      this.username = username;
      }
      public boolean equals(Object o) {
      if(this == o) return true;
      if(!(o instanceof CustomRevisionEntity)) return false;
      CustomRevisionEntity that = (CustomRevisionEntity) o;
      if(id != that.id) return false;
      if(timestamp != that.timestamp) return false;
      if(timestamp != that.timestamp) return false;
      if(username != that.username) return false;
      return true;
      }
      public int hashCode() {
      int result;
      result = id;
      //TODO: create more unique hash code
      result = 31 * result + (int) (timestamp ^ (timestamp >>> 32)) + username.hashCode();
      return result;
      }
      }
      package com.csbi.samples.audit;
      
      import org.hibernate.envers.RevisionNumber;
      import org.hibernate.envers.RevisionTimestamp;
      import org.hibernate.envers.RevisionEntity;
      
      import javax.persistence.Id;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Entity;
      import javax.persistence.Table;
      
      @Entity
      @Table(name="REVISIONS")
      @RevisionEntity(CustomRevisionListener.class)
      public class CustomRevisionEntity {
          @Id
          @GeneratedValue
          @RevisionNumber
          private int id;
      
          @RevisionTimestamp
          private long timestamp;
      
          private String username;
      
          public int getId() {
              return id;
          }
      
          public void setId(int id) {
              this.id = id;
          }
      
          public long getTimestamp() {
              return timestamp;
          }
      
          public void setTimestamp(long timestamp) {
              this.timestamp = timestamp;
          }
      
          public String getUsername() {
              return username;
          }
      
          public void setUsername(String username) {
              this.username = username;
          }
      
          public boolean equals(Object o) {
              if(this == o) return true;
              if(!(o instanceof CustomRevisionEntity)) return false;
                 
              CustomRevisionEntity that = (CustomRevisionEntity) o;
                 
              if(id != that.id) return false;
              if(timestamp != that.timestamp) return false;
              if(timestamp != that.timestamp) return false;
              if(username != that.username) return false;
                 
              return true;
          }
                 
          public int hashCode() {
              int result;
              result = id;
           result = 31 * result + (int) (timestamp ^ (timestamp >>> 32)) + username.hashCode();
           return result;
          }
      }
      

       

       

      And also custom listener:

       

       

      package com.csbi.samples.audit;
      import org.hibernate.envers.RevisionListener;
      
      public class CustomRevisionListener implements RevisionListener {
           
          public void newRevision(Object revisionEntity) {
              CustomRevisionEntity revision = (CustomRevisionEntity) revisionEntity;
              revision.setUsername("username"); //for testing
          }
      
      }
      

       

       

       

      DEBUG: org.hibernate.envers.configuration.metadata.AuditMetadataGenerator - Generating first-pass auditing mapping for entity com.csbi.samples.domain.Property.
      DEBUG: org.hibernate.envers.configuration.metadata.AuditMetadataGenerator - Generating second-pass auditing mapping for entity com.csbi.samples.domain.Property.
      INFO : org.hibernate.cfg.HbmBinder - Mapping class: com.csbi.samples.domain.Property_AUD -> PROPERTIES_AUD
      INFO : org.hibernate.cfg.HbmBinder - Mapping class: org.hibernate.envers.DefaultRevisionEntity -> REVINFO
      

       

      Take a look at the last line in the output given.

      There is still DefaultRevisionEntity mapped instead of CustomRevisionEntity.

       

      I have no idea atm what is wrong. Any suggestions?

       


        • 1. Re: Custom revision entity problem
          hernanbolido

          Hi!

           

          Are you adding your custom revision entity as an entity for hibernate like any other entity in your system?

           

          Regards. Hernán.

          • 2. Re: Custom revision entity problem
            konyshev

            Hernán Chanfreau wrote:

             

            Hi!

             

            Are you adding your custom revision entity as an entity for hibernate like any other entity in your system?

             

            Regards. Hernán.

             

            I do not add any entity. They all are marked by @Entity annotation, it's enough. And I call persistence manager manually for CRUD.

            Is there any ideas if I say that we use Spring IoC and Spring MVC?

             

            As I understand annotations processor while processing @RevisionEntity should set my entity class to use by enver. But it doesn't happen. Log has only these 4 lines about Envers, that's it!

            • 3. Re: Custom revision entity problem
              adamw

              If you are using hbm2ddl update or create-drop, is the REVISIONS table created? That is, does Hibernate (plain HIbernate, not Envers), "see" the CustomRevisionEntity entity? You may also using it in a query to see if it works and is recognized by Hibernate.

               

              Adam

              • 4. Re: Custom revision entity problem
                obastard

                I had this problem because my RevisionEntity wasn't listed anywhere. You both need the annotation, and you need that entity listed as an entity in your model. Could that be your problem?

                • 5. Re: Custom revision entity problem
                  fmassart

                  I had the same problem with a version 1.0.0 of Envers.
                  I only added my RevisionEntity in the "persistence.xml" file in order to solve the issue