Spring Data(Spring Boot)和Joda Time字段映射问题

时间:2017-01-26 18:20:09

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

我有一些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,但没有任何变化。我该如何解决这个问题?

==================== EDIT 调试使我进入了班级AbstractUserTypeHibernateIntegrator

的第192行
 String isEnabled = (String)sessionFactory.getProperties().get("jadira.usertype.autoRegisterUserTypes");

我的调试器说分配给isEnabled的值是&#34; true&#34;。但是,执行会跳转到finally子句:ConfigurationHelper.setCurrentSessionFactory((SessionFactory)null)

3 个答案:

答案 0 :(得分:3)

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>

答案 1 :(得分:0)

上述问题的原因是您忘记添加entitiyManager依赖项。

尝试添加以下依赖项

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.8.Final</version>
</dependency>

答案 2 :(得分:0)

这对于春季启动对我有用,问题是Spring启动版本1.5.4 data-jpa启动器有hibernate-core和hibernate-entitymanager版本5.0.12,这与jadira版本6.0.1.GA不兼容所以我们必须在pom.xml中覆盖它们并至少提供版本5.2.0.Final(我在代码中使用了5.2.10.Final,因为这是写入时的最新版本。)

    <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>

    <!-- Spring boot version 1.5.4  has  hibernate-core 5.0.12 which is incompatible with jadira version 6.0.1.GA -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.10.Final</version>
    </dependency>
    <!-- Spring boot version 1.5.4  has  hibernate-entitymanager 5.0.12 which is incompatible with jadira version 6.0.1.GA -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.10.Final</version>
    </dependency>