0

我尝试使用 Hibernate Envers 来记录实体历史。但失败了。

我的项目使用了 Spring 3.1、JPA 和 Spring Data JPA(Hibenrate 4.1)。

有一个基础类。

代码:

@javax.persistence.MappedSuperclass
public abstract class BaseEntity implements Serializable {



    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", columnDefinition = "INT unsigned", nullable = false)
    @RevisionNumber
    public Long id;



    @Version
    @Column(name = "jpaversion", nullable = false, columnDefinition = "INT unsigned")
   @RevisionTimestamp
    public Long version;



    public Long getId() {
   return id;
    }



    public void setId(Long id) {
   this.id = id;
    }



    public Long getVersion() {
   return version;
    }



    public void setVersion(Long version) {
   this.version = version;
    }



    @Override
    public int hashCode() {
   final int prime = 31;
   int result = 1;
   result = prime * result + ((id == null) ? 0 : id.hashCode());
   return result;
    }



    @Override
    public boolean equals(Object obj) {
   if (this == obj)
       return true;
   if (obj == null)
       return false;
   if (getClass() != obj.getClass())
       return false;
   BaseEntity other = (BaseEntity) obj;
   if (id == null) {
       if (other.id != null)
      return false;
   } else if (!id.equals(other.id))
       return false;
   return true;
    }



}

当我将 @RevisionEntity 添加到我的实体时。

代码:

@Entity
@RevisionEntity
@Table(name = "user", uniqueConstraints = {
    @UniqueConstraint(name = "UQ_gsmPrefix_gsmNumber", columnNames = {"gsm_prefix", "gsm_number"}),
    @UniqueConstraint(name = "UQ_email", columnNames = "email"),
    @UniqueConstraint(name = "UQ_username", columnNames = "username")})
@NamedQueries({
    @NamedQuery(name = "User.findByUsername", query = "select u from User u where u.username=:username"),
    @NamedQuery(name = "User.findByEmail", query = "select u from User u where u.email=:email")})
@Access(AccessType.FIELD)
public class User extends BaseEntity implements UserDetails, Serializable {

代码:

@Entity @Table(name = "address") @RevisionEntity 公共类地址扩展 com.dna.bifincan.model.BaseEntity

当我将项目部署到tomcat中时,我得到了这样的异常。

引用:

org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0”的bean时出错:bean初始化失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 URL [jar:file:/E:/MyWorks/bifincan/project/bifincan-website/target/fi/WEB-INF 中定义名称为“entityManagerFactory”的 bean 创建错误/lib/bifincan-library-1.0-SNAPSHOT.jar!/common/data-access.xml]:调用 init 方法失败;嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: bifincan] Unable to build EntityManagerFactory at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527) at org.springframework.beans。AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java: 1110) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 在 java.lang.Thread.run(Thread.java:722) 引起:org.springframework.beans.factory.BeanCreationException:错误在 URL [jar:file:/E:/MyWorks/bifincan/project/bifincan-website/target/fi/WEB-INF/lib/bifincan-library-1.0-SNAPSHOT.jar!/ common/data-access.xml]: init 方法调用失败;嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: @RevisionEntity 只能注释一个实体!在 org.hibernate.envers.configuration.RevisionInfoConfiguration.configure(RevisionInfoConfiguration.java:264) 在 org.hibernate.envers.configuration.AuditConfiguration.(AuditConfiguration.java:102) 在 org.hibernate.envers.configuration.AuditConfiguration.getFor( AuditConfiguration.java:165) 在 org.hibernate.cfg.Configuration 的 org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:302) 的 org.hibernate.envers.event.EnversIntegrator.integrate(EnversIntegrator.java:64)。 buildSessionFactory(Configuration.java:1740) at org.hibernate.ejb.EntityManagerFactoryImpl.(EntityManagerFactoryImpl.java:88) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904) ... 还有 71 个

显然,我同时使用了@Entity 和@RevisitonEntity,但它仍然会报告此信息。

我阅读了Hibernate Dev Guide中相关部分的文档,似乎persistence.xml或hibernate配置中不需要额外的配置。我在 Spring 项目中遗漏了什么。

任何帮助表示赞赏。

4

1 回答 1

1

为什么你需要用 注释你的实体@RevisionEntity

@RevisionEntity是代表修订本身的特殊目的实体。如果您想让 Envers 审核您的实体,请使用@Audited.

于 2012-06-21T07:38:40.397 回答