我有一些带有 Joda DateTime 字段的实体。 当尝试启动应用程序时,出现以下错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Map;
[...]
Caused by: java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Map;
at org.jadira.usertype.spi.shared.AbstractUserTypeHibernateIntegrator.integrate(AbstractUserTypeHibernateIntegrator.java:192) ~[usertype.spi-6.0.1.GA.jar:na]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:280) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
我试图把
spring.jpa.properties.jadira.usertype.autoRegisterUserTypes=true
在 application.properties 中,它不起作用。所以我将 Hibernate 注释添加到我的实体类中:
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime from;
但没有成功。
我的pom.xml(部分):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>org.jadira.usertype</groupId>
<artifactId>usertype.core</artifactId>
<version>6.0.1.GA</version>
</dependency>
<!-- Jackson json data bind -->
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
</dependency>
我还尝试了 Jadira usertype.core 6.0.0.GA、5.0.0.GA 和 4.0.0.GA,但没有任何变化。我该如何解决这个问题?
====================编辑
调试将我引导至 AbstractUserTypeHibernateIntegrator
String isEnabled = (String)sessionFactory.getProperties().get("jadira.usertype.autoRegisterUserTypes");
我的调试器说分配给 isEnabled
的值是“true”。但是,执行跳转到finally子句: ConfigurationHelper.setCurrentSessionFactory((SessionFactory)null)
最佳答案
SessionFactoryImplementor.getProperties() 的签名在 5.2 中更改为返回 Map 而不是 Properties。如果 hibernate 版本低于该版本,这会导致 NoSuchMethodError。并且确保使用与 Hibernate 5.2 兼容的最新 jadira jar
在 pom.xml 中添加这些依赖项
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.jadira.usertype</groupId>
<artifactId>usertype.core</artifactId>
<version>6.0.1.GA</version>
</dependency>
关于spring - Spring Data (Spring Boot) 和 Joda Time 字段映射的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41880292/