最近项目升级到 Spring 3.3.x 后,提示下面的 JPA 错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Type com.vladmihalcea.hibernate.type.array.StringArrayType not present
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1806)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:365)
... 99 common frames omitted
Caused by: java.lang.TypeNotPresentException: Type com.vladmihalcea.hibernate.type.array.StringArrayType not present
at java.base/sun.reflect.annotation.TypeNotPresentExceptionProxy.generateException(TypeNotPresentExceptionProxy.java:47)
at java.base/sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:89)
at jdk.proxy2/jdk.proxy2.$Proxy97.converter(Unknown Source)
at org.hibernate.boot.model.internal.AttributeConversionInfo.<init>(AttributeConversionInfo.java:44)
at org.hibernate.boot.model.internal.ClassPropertyHolder.collectAttributeConversionInfo(ClassPropertyHolder.java:127)
at org.hibernate.boot.model.internal.ClassPropertyHolder.collectAttributeConversionInfo(ClassPropertyHolder.java:104)
at org.hibernate.boot.model.internal.ClassPropertyHolder.buildAttributeConversionInfoMap(ClassPropertyHolder.java:93)
at org.hibernate.boot.model.internal.ClassPropertyHolder.<init>(ClassPropertyHolder.java:68)
at org.hibernate.boot.model.internal.ClassPropertyHolder.<init>(ClassPropertyHolder.java:77)
at org.hibernate.boot.model.internal.PropertyHolderBuilder.buildPropertyHolder(PropertyHolderBuilder.java:35)
at org.hibernate.boot.model.internal.EntityBinder.bindEntityClass(EntityBinder.java:240)
at org.hibernate.boot.model.internal.AnnotationBinder.bindClass(AnnotationBinder.java:399)
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:259)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:281)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:324)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1431)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1502)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:75)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:390)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1802)
... 106 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.vladmihalcea.hibernate.type.array.StringArrayType
最后的错误信息为 Hibernate 的类型错。
问题和解决
最后,问题是出在:
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-55</artifactId>
<version>2.21.1</version>
</dependency>
上。
如果是 Spring 3 的 Sping 版本,应该需要用 hibernate-types-60
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-60</artifactId>
<version>2.21.1</version>
</dependency>
而且讨厌的是,这个是运行时错误,只有在启动项目后才能看到。