从3迁移到Hibernate 5

Syd*_*abu 5 hibernate jodatime java-8 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;
Run Code Online (Sandbox Code Playgroud)

小智 5

我从 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;
Run Code Online (Sandbox Code Playgroud)

到 Java 8 日期

@Type(type="org.hibernate.type.LocalDateTimeType")
private java.time.LocalDateTime startDate;

@Type(type="org.hibernate.type.ZonedDateTimeType")
private java.time.ZonedDateTime creationDate;
Run Code Online (Sandbox Code Playgroud)

如果有,请删除 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>
Run Code Online (Sandbox Code Playgroud)

并添加 hibernate-java8

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-java8</artifactId>
        <version>5.0.4.Final</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

您可以查看有关如何将 Joda 时间类型转换为 Java 日期时间的更多详细信息http://blog.joda.org/2014/11/converting-from-joda-time-to-javatime.html

  • 用 jadira 注释替换 joda.time @type 注释似乎没有那么侵入性。 (2认同)