我正在从 3 迁移到 Hibernate 5.0.3.Final。在 3.x 中,我使用 joda-time 将 LocalDateTime 保留在 Oracle DB 中。现在我发现 hibernate 5 不支持 joda-time。请让我知道最好的替代方案是什么?
这是代码示例。
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
public class ComponentHistory {
@Column(name = EntityConstants.CREATED_BY_COLUMN_NAME)
private String createdBy;
@Column(name = EntityConstants.CREATED_DATE_COLUMN_NAME)
@Type(type = "org.joda.time.contrib.hibernate.PersistentLocalDateTime")
private LocalDateTime createdDate;
@Column(name = EntityConstants.UPDATED_BY_COLUMN_NAME)
private String updatedBy;
@Column(name = EntityConstants.UPDATED_DATE_COLUMN_NAME)
@Type(type = "org.joda.time.contrib.hibernate.PersistentLocalDateTime")
private LocalDateTime updatedDate;
我从 Hibernate 4 迁移到 5,所以也许适合您,我所做的就是删除所有 Joda Time 依赖项并将类替换为新的 Java Date Api,如下所示。
从乔达时间
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime startDate;
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime creationDate;
到 Java 8 日期
@Type(type="org.hibernate.type.LocalDateTimeType")
private java.time.LocalDateTime startDate;
@Type(type="org.hibernate.type.ZonedDateTimeType")
private java.time.ZonedDateTime creationDate;
如果有 Maven 依赖项,请删除它
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time-hibernate</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.jadira.usertype</groupId>
<artifactId>usertype.core</artifactId>
<version>3.1.0.CR8</version>
</dependency>
并添加 hibernate-java8
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>5.0.4.Final</version>
</dependency>
您可以查看有关如何将 Joda 时间类型转换为 Java 日期时间的更多详细信息http://blog.joda.org/2014/11/converting-from-joda-time-to-javatime.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)