I have a JPA entity that looks something like this. Note the @EntityListeners
annotation:
import javax.persistence.EntityListeners;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@XmlRootElement
@MappedSuperclass
@EntityListeners( { AuditingEntityListener.class } )
public class BaseEntity implements Serializable
{
//body
}
I'm using Hibernate as the JPA provider.
I use Spring's LocalContainerEntityManagerFactoryBean
to create the JPA EntityManager
. I use the setPersistenceUnitPostProcessors
method to set a PersistenceUnitPostProcessor
instance that registers the entity classes in the postProcessPersistenceUnitInfo
method. (I know it's not Spring Boot, but that's a topic for another time).
After setting a DataSource
and other properties on the LocalContainerEntityManagerFactoryBean
, calling the afterPropertiesSet
method throws this exception:
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)
I commented out @EntityListeners( { AuditingEntityListener.class } )
annotation on the entity class listed above.
After that, the LocalContainerEntityManagerFactoryBean
's afterPropertiesSet()
method did not throw an error, and it produced a usable EntityManager
instance (I was able to use it to do a basic select of an entity). So, at least I know that the rest of the setup works correctly.
Can anybody tell me why I'm getting the error when using the @EntityListeners
annotation, and how I can fix it? As far as I can tell it's in the runtime classpath (the javax.persistence-api-2.2.jar
file is in the webapp's WEB-INF/lib
directory) and so is the AuditingEntityListener
class (in the spring-data-jpa-1.11.6.RELEASE.jar
file).
If it's helpful, these are the Gradle entries I'm using to get the dependencies:
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"
What can I do to get rid of the error when using the @EntityListeners
annotation?
Thank you!!!
I did some more testing. I found that this annotation did not produce an error:
@EntityListeners( { } )
So the AuditingEntityListener
appeared to be the culprit.
More web searching turned up this issue in Spring. It mentioned the spring-aspects
project as being necessary for the runtime code weaving of AuditingEntityListener
, so I added this to my Gradle build file:
compile "org.springframework:spring-aspects:4.3.10.RELEASE"
After a rebuild and redeploy the problem went away! Yay!