Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating To Hibernate 5 from 3

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;
like image 713
Sydubabu Avatar asked Nov 16 '15 10:11

Sydubabu


1 Answers

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

like image 59
user2669657 Avatar answered Oct 09 '22 12:10

user2669657