Why Hibernate Envers is ignoring my custom Revisio

2019-04-10 19:00发布

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?

3条回答
Lonely孤独者°
2楼-- · 2019-04-10 19:34

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.

查看更多
贼婆χ
3楼-- · 2019-04-10 19:36

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

our.basepackage.otherpackage.AuditedRevisionEntity

查看更多
相关推荐>>
4楼-- · 2019-04-10 19:58

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

@EntityScan(basePackages = {"yourpackage.entities","yourpackage.envers.entity"})
查看更多
登录 后发表回答