Migrating To Hibernate 5 from 3

混江龙づ霸主 提交于 2020-01-13 10:33:16

问题


I am migrating to Hibernate 5.0.3.Final from 3. In 3.x I am using joda-time to persist LocalDateTime in oracle DB. Now I am seeing that hibernate 5 doesn't have a support to joda-time. Please let me know what would be the best alternative for it?

Here is code sample.

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:


I migrated from Hibernate 4 to 5 so maybe can be suitable for you, what I did was remove all Joda Time dependencies and replace the classes to new Java Date Api, like this.

From Joda Time

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

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

To 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;

Remove maven dependencies if you have it

    <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>

And add hibernate-java8

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-java8</artifactId>
        <version>5.0.4.Final</version>
    </dependency>

You can see more details about how to convert Joda Time Type to Java Date Time http://blog.joda.org/2014/11/converting-from-joda-time-to-javatime.html



来源:https://stackoverflow.com/questions/33732785/migrating-to-hibernate-5-from-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!