hibernate 4 and joda-time

NimChimpsky picture NimChimpsky · Jan 23, 2012 · Viewed 34.5k times · Source

are they happily married ?

I am using the latest version of hibernate (4) and version 1.3 of joda-time hibernate support, which I also believe to be the current latest release.

Everything seems to be working OK (date columns created as expected) when using annotations :

@Column
@Type(type="org.joda.time.contrib.hibernate.PersistentLocalDate")
private LocalDate myDate; 

Are their any known problems with using these versions together ?

Update Well turns out the columns get created but unable to populate with any data :

Handler processing failed; nested exception is java.lang.AbstractMethodError: org.joda.time.contrib.hibernate.PersistentLocalDateTime.nullSafeSet

They are incompatible, and I should be using usertype. See answer below.

Answer

NimChimpsky picture NimChimpsky · Jan 24, 2012

A distinct paucity of documentation, means it might be helpful for me to write down the steps required for integration. Make sure your libraries are up to date.

You'll need : [assuming you already have hibernate4]

Latest version of joda-time

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.0</version>
</dependency>

and usertype lib

<dependency>
    <groupId>org.jadira.usertype</groupId>
    <artifactId>usertype.core</artifactId>
    <version>3.0.0.CR1</version>
</dependency>

Then use the following in entity classes (doesn't have to be LocalDateTime, could be any of the persisted classes available) :

import org.joda.time.LocalDateTime;

and for column definition:

@Column(name="updated", nullable = false)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime updated;