简体   繁体   English

Spring JPA Hibernate和AttributeConverter

[英]Spring JPA Hibernate and AttributeConverter

I have a small application (spring 4.0, jpa 2.1, hibernate 5.0.2) and had been using "old" java.util.Date* classes as well as java.sql.Date*. 我有一个小应用程序(spring 4.0,jpa 2.1,hibernate 5.0.2),并且一直在使用“旧的” java.util.Date *类以及java.sql.Date *。 Now I wanted to use java.time instead and read that it would work with an AttributeConverter. 现在我想使用java.time并读取它可以使用AttributeConverter。 Unfortunately this doesn't seem to work. 不幸的是,这似乎不起作用。 The moment I try to read the database object with a timestamp (the doa has an equivalent of java.time.localdatetime) I get an exception. 当我尝试使用时间戳读取数据库对象的时候(DOA具有等效的java.time.localdatetime),我得到了一个异常。 It seems the Converter isn't being used at all even though the annotation is there. 即使有注释,似乎也没有使用Converter。 I only have an applicationContext.xml and no persistence.xml so where would I tell jpa to use the Converter (if the annotation isn't enough)? 我只有一个applicationContext.xml,没有persistence.xml,所以我要告诉jpa在哪里使用Converter(如果注释不够的话)? How can I see that the AttributeConverter is picked up by jpa at all? 我怎样才能看到AttributeConverter被jpa接走了?

Thanks in advance, John. 预先感谢约翰。

@Converter is available in JPA2.1 . @Converter在JPA2.1中可用。

Try to change your dependency config: 尝试更改依赖项配置:

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

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.7.2.RELEASE</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.5.Final</version>
    </dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM