引起:org.hibernate.MappingException:只有一个实体可以用@RevisionEntity注释

时间:2012-06-21 07:04:09

标签: hibernate

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

我的项目使用了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(姓名="地址")       @RevisionEntity       公共类地址扩展com.dna.bifincan.model.BaseEntity

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

引用:

  

org.springframework.beans.factory.BeanCreationException:错误   用名字创建bean   ' org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0&#39 ;:   bean的初始化失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名为&entityManagerFactory'的bean在URL中定义   [JAR:文件:/ 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:bifincan]   无法构建EntityManagerFactory       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)       在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:294)       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)       在org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:728)       在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:449)       在org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)       在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)       在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)       在org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4779)       at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)       在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)       在org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:895)       在org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:871)       在org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)       在org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:649)       在org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:530)       在org.apache.catalina.startup.HostConfig.check(HostConfig.java:1471)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:601)       at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)       at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)       at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:791)       在org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1436)       在org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:856)       在org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:353)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:621)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:722)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)       在org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:581)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)       在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)       在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)       at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:565)       在org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:309)       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:603)       在java.lang.Thread.run(Thread.java:722)       引起:org.springframework.beans.factory.BeanCreationException:错误   创建名为&entityManagerFactory'的bean在URL中定义   [JAR:文件:/ 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:bifincan]   无法构建EntityManagerFactory       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)       在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:294)       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)       at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:400)       在org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:275)       at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:139)       在org.springframework.dao.support.PersistenceExceptionTranslationInterceptor。(PersistenceExceptionTranslationInterceptor.java:79)       在org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor。(PersistenceExceptionTranslationAdvisor.java:70)       at org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:103)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1475)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1443)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)       ......还有50个       引起:javax.persistence.PersistenceException:[PersistenceUnit:bifincan]无法构建EntityManagerFactory       在org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)       在org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)       在org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)       在org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:268)       在org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)       ......还有65个       引起:org.hibernate.MappingException:只有一个实体可以用@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.envers.event.EnversIntegrator.integrate(EnversIntegrator.java:64)       在org.hibernate.internal.SessionFactoryImpl。(SessionFactoryImpl.java:302)       在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1740)       在org.hibernate.ejb.EntityManagerFactoryImpl。(EntityManagerFactoryImpl.java:88)       在org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)       ... 71更多

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

我在Hibernate开发指南中阅读了相关部分的文档,似乎不需要在persistence.xml或hibernate配置中进行额外配置。是否有我在Spring项目中省略的东西。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

为什么需要使用@RevisionEntity注释您的实体?

@RevisionEntity是一个特殊目的实体,代表修订本身。如果您想让您的实体由Envers审核,请使用@Audited注释它们。