我有一个看起来像这样的 JPA 实体。注意@EntityListeners
注释:
import javax.persistence.EntityListeners;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@XmlRootElement
@MappedSuperclass
@EntityListeners( { AuditingEntityListener.class } )
public class BaseEntity implements Serializable
{
//body
}
我使用 Hibernate 作为 JPA 提供程序。
我使用 SpringLocalContainerEntityManagerFactoryBean
来创建 JPA EntityManager
。我使用该setPersistenceUnitPostProcessors
方法设置了一个PersistenceUnitPostProcessor
实例,该实例在该方法中注册了实体类postProcessPersistenceUnitInfo
。(我知道这不是 Spring Boot,但这是另一个话题)。
在 上设置 aDataSource
和其他属性后LocalContainerEntityManagerFactoryBean
,调用该afterPropertiesSet
方法会引发此异常:
java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:724)
at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:531)
at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:355)
at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:286)
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)
at java.lang.Class.createAnnotationData(Class.java:3521)
at java.lang.Class.annotationData(Class.java:3510)
at java.lang.Class.getAnnotation(Class.java:3415)
at java.lang.reflect.AnnotatedElement.isAnnotationPresent(AnnotatedElement.java:258)
at java.lang.Class.isAnnotationPresent(Class.java:3425)
at org.hibernate.annotations.common.reflection.java.JavaAnnotationReader.isAnnotationPresent(JavaAnnotationReader.java:50)
at org.hibernate.annotations.common.reflection.java.JavaXAnnotatedElement.isAnnotationPresent(JavaXAnnotatedElement.java:60)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.categorizeAnnotatedClass(AnnotationMetadataSourceProcessorImpl.java:116)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.<init>(AnnotationMetadataSourceProcessorImpl.java:105)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.<init>(MetadataBuildingProcess.java:147)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:141)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:858)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359)
我注释掉了@EntityListeners( { AuditingEntityListener.class } )
上面列出的实体类的注释。
之后,LocalContainerEntityManagerFactoryBean
'safterPropertiesSet()
方法没有抛出错误,它生成了一个可用的EntityManager
实例(我能够使用它来对实体进行基本选择)。所以,至少我知道其余的设置工作正常。
谁能告诉我为什么在使用@EntityListeners
注释时会出现错误,以及如何解决?据我所知,它在运行时类路径中(javax.persistence-api-2.2.jar
文件在 webapp 的WEB-INF/lib
目录中),AuditingEntityListener
类(在spring-data-jpa-1.11.6.RELEASE.jar
文件中)也是如此。
如果有帮助,这些是我用来获取依赖项的 Gradle 条目:
compile "org.springframework:spring-core:4.3.7.RELEASE"
compile "org.springframework:spring-beans:4.3.7.RELEASE"
compile "org.springframework:spring-context:4.3.7.RELEASE"
compile "org.springframework.data:spring-data-jpa:1.11.6.RELEASE"
compile "javax.persistence:javax.persistence-api:2.2"
compile "org.hibernate:hibernate-core:5.2.10.Final"
@EntityListeners
使用注释时我该怎么做才能摆脱错误?
谢谢!!!