从3

时间:2015-11-16 10:09:45

标签: hibernate java-8 jodatime hibernate-5.x

我从3.迁移到Hibernate 5.0.3.Final 3.在3.x我使用joda-time在oracle DB中持久化LocalDateTime。现在我看到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;

1 个答案:

答案 0 :(得分:3)

我从Hibernate 4迁移到5,所以可能适合你,我所做的就是删除所有Joda Time依赖项并将类替换为新的Java Date Api,就像这样。

从Joda时间

@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime startDate;

@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime creationDate;

到Java 8 Date

@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

的更多详细信息