3

I'm developing an application using JPA 2.1 (supported by hibernate 4.2.11) with spring 4.0.2. We are using Envers for auditing changes in project entities. That is working fine. The problem comes when we try to use custom Revision Entity as Envers documentation says: http://docs.jboss.org/hibernate/core/4.1/devguide/en-US/html/ch15.html#envers-revisionlog

We have made a custom class and a custom listener as pointed in the documentation but it seems that they are totally ignored by Hibernate. The custom classes:

@Entity
@RevisionEntity(AuditingRevisionListener.class)
public class AuditedRevisionEntity extends DefaultRevisionEntity {
  private String username;

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }    
}


public class AuditingRevisionListener implements RevisionListener {
  private static Log log = LogFactory.getLog(AuditingRevisionListener.class.getName());

  @Override
  public void newRevision(Object revisionEntity) {
    AuditedRevisionEntity revEntity = (AuditedRevisionEntity) revisionEntity;
    String userName = SecurityContextHolder.getContext().getAuthentication().getName();
    revEntity.setUsername(userName);
  }

}

It's like hibernate is not taking into account this classes because it should be enough tom make it work to use the annotation: @RevisionEntity(AuditingRevisionListener.class). The other annotated entities are well recognized but this is simply ignored. We have in our spring config (we dont use persistence.xml) the base packages to be scanned when loading spring context:

  <context:component-scan base-package="our.basepackage" />

Is this enough?

We have also tried another aproach. To set manually the revision listener with this property in EntityManagerFactory configuration

    <prop key="org.hibernate.envers.revision_listener">our.basepackage.AuditingRevisionListener</prop>

But we get a classcastexception in the first line of newRevision method because the parameter revisionEntity is not of class AuditedRevisionEntity. Again is like the class AuditedRevisionEntity is not loaded.

I supose it's simple question but i'm unable to guess why @RevisionEntity annotation is beeing ignored. Any idea?

Ricardo Vila
  • 1,626
  • 1
  • 18
  • 34

3 Answers3

7

Finally i got where the problem was. My suspects were right, hibernate was ignoring my Envers classes because of this param in the EntityManagerFactory configuration:

<property name="packagesToScan" value="our.basepackage.otherpackage" />

We need to tell hibernate to check for 'our.basepackage'. Silly problem easy solution.

Ricardo Vila
  • 1,626
  • 1
  • 18
  • 34
  • Can you share where did you tell hibernate to check for 'your.base.package', I have the same problem. Thank you – OJVM Oct 20 '14 at 15:56
  • After debugging envers, i found that the 'revinfo' table was not working because in my entity i didn'n add the @Table annotation, so the name of the revision table was the same as my entity name. after adding @Table(name = 'REVINFO') everything started to work ok. – OJVM Oct 20 '14 at 16:19
3

For any guy who is working with SpringBoot you may change this

@EntityScan(basePackages = {"yourpackage.entities","yourpackage.envers.entity"})
Pwnstar
  • 2,333
  • 2
  • 29
  • 52
-1

I got problems even after the above steps. I have added AuditedRevisionEntity to persistance.xml file to resolve the issue.

our.basepackage.otherpackage.AuditedRevisionEntity