我有一个看起来像这样的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提供程序。
我使用Spring的
LocalContainerEntityManagerFactoryBean
创建JPA EntityManager
。我使用setPersistenceUnitPostProcessors
方法设置一个PersistenceUnitPostProcessor
实例,该实例在postProcessPersistenceUnitInfo
方法中注册实体类。 (我知道这不是Spring Boot,但这是另一个话题)。在
DataSource
上设置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
的afterPropertiesSet()
方法没有引发错误,并且产生了一个可用的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
注释时,我该怎么办才能消除错误?谢谢!!!
最佳答案
我做了一些测试。我发现此注释未产生错误:
@EntityListeners( { } )
因此,
AuditingEntityListener
似乎是罪魁祸首。出现了更多的Web搜索this issue in Spring。它提到
spring-aspects
项目是编织AuditingEntityListener
的运行时代码所必需的,因此我将其添加到了Gradle构建文件中:compile "org.springframework:spring-aspects:4.3.10.RELEASE"
重建并重新部署后,问题就消失了!好极了!