为什么Hibernate Envers忽略了我的自定义RevisionEntity? [英] Why Hibernate Envers is ignoring my custom RevisionEntity?

查看:284
本文介绍了为什么Hibernate Envers忽略了我的自定义RevisionEntity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用JPA 2.1(受hibernate 4.2.11支持)和Spring 4.0.2的应用程序.我们正在使用Envers审核项目实体中的更改.很好. 当我们尝试使用自定义版本实体作为Envers文档时出现问题:

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

我们已经按照文档中的说明创建了一个自定义类和一个自定义侦听器,但是似乎它们被Hibernate完全忽略了.自定义类:

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);
  }

}

这就像休眠状态不考虑此类,因为它足以使它能够使用注释:@RevisionEntity(AuditingRevisionListener.class).其他带注释的实体可以很好地识别,但是可以忽略不计.在我们的spring配置中(不使用persistence.xml),我们在加载spring上下文时要扫描基本软件包:

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

够了吗?

我们还尝试了另一种方式.要在EntityManagerFactory配置中使用此属性手动设置修订侦听器

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>

但是在newRevision方法的第一行中我们得到了一个classcastexception,因为参数versionEntity不是AudgedRevisionEntity类.再次像类AuditedRevisionEntity未加载.

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.

我认为这是一个简单的问题,但是我无法猜测为什么@RevisionEntity注释会被忽略. 任何的想法?

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

推荐答案

最后,我明白了问题所在.我的怀疑者是对的,由于EntityManagerFactory配置中的以下参数,休眠状态忽略了我的Envers类:

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

我们需要告诉hibernate检查'our.basepackage'. 愚蠢的问题容易解决.

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

这篇关于为什么Hibernate Envers忽略了我的自定义RevisionEntity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆