@EntityListeners注释导致TypeNotPresentExceptionProxy错误

时间:2017-08-23 18:49:13

标签: hibernate jpa spring-data spring-data-jpa hibernate-jpa

我有一个看起来像这样的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的{​​{1}}方法抛出错误,并生成了一个可用的afterPropertiesSet()实例(我是能够用它来做一个实体的基本选择)。所以,至少我知道其余的设置工作正常。

任何人都可以告诉我为什么我在使用EntityManager注释时遇到错误,以及我如何修复它?据我所知,它在运行时类路径中(@EntityListeners文件位于webapp的javax.persistence-api-2.2.jar目录中),WEB-INF/lib类也是如此(在AuditingEntityListener文件。)

如果它有用,这些是我用来获取依赖项的Gradle条目:

spring-data-jpa-1.11.6.RELEASE.jar

使用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" 注释时,我该怎么做才能消除错误?

谢谢!!!

1 个答案:

答案 0 :(得分:2)

我做了一些测试。我发现这个注释会产生错误:

@EntityListeners( { } )

所以AuditingEntityListener似乎是罪魁祸首。

更多网络搜索出现this issue in Spring。它提到spring-aspects项目是AuditingEntityListener的运行时代码编织所必需的,所以我将它添加到我的Gradle构建文件中:

compile "org.springframework:spring-aspects:4.3.10.RELEASE"

重建和重新部署后问题消失了!耶!