Migrating To Hibernate 5 from 3
Asked Answered
E

1

6

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;
Emlin answered 16/11, 2015 at 10:9 Comment(0)
Q
6

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

Quietism answered 5/1, 2016 at 13:15 Comment(1)
Replacing the joda.time @type annotations with the jadira ones seems a lot less invasive.Bodoni

© 2022 - 2024 — McMap. All rights reserved.