【问题标题】:Spring JPA Hibernate and AttributeConverterSpring JPA Hibernate 和 AttributeConverter
【发布时间】:2015-02-12 08:18:27
【问题描述】:
我有一个小型应用程序(spring 4.0、jpa 2.1、hibernate 5.0.2)并且一直在使用“旧”java.util.Date* 类以及 java.sql.Date*。现在我想改用 java.time 并读到它可以与 AttributeConverter 一起使用。
不幸的是,这似乎不起作用。当我尝试使用时间戳读取数据库对象时(doa 相当于 java.time.localdatetime),我得到了一个异常。
即使注释在那里,似乎根本没有使用转换器。我只有一个 applicationContext.xml 而没有 persistence.xml 那么我在哪里告诉 jpa 使用转换器(如果注释不够)?
我怎样才能看到 AttributeConverter 完全被 jpa 拾取了?
提前致谢,
约翰。
【问题讨论】:
标签:
spring
hibernate
jpa-2.1
【解决方案1】:
@Converter 在 JPA2.1 中可用。
尝试改变你的依赖配置:
<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>