63

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.

4
  • You just told us they did work together, didn't you?
    – skaffman
    Jan 23, 2012 at 16:23
  • @skaffman I haven't tested anything other than column creation ... it was my understanding that previous versions (of joda-time lib) had to be recompiled against the newer version of hibernate. This rang an alarm - hence the question ... Jan 23, 2012 at 16:25
  • They are not happily married, hibernate is having other relationships...and joda.. :/.. She is hurt
    – nobalG
    Jan 19, 2015 at 5:26
  • Also be carefull when you import LocalDate because its also avaiable in Java 8 now.
    – jgr
    Sep 25, 2015 at 11:34

2 Answers 2

96

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;
7
  • 1
    How do we know what type it maps to in our database of choice? May 30, 2012 at 20:33
  • Right, so use auto generation of tables and look at the output? May 30, 2012 at 20:39
  • 2
    yeah thats what i would do - digging through the hibernate docs is a bit of a challenge May 30, 2012 at 20:42
  • 2
    In case someone has the same problem: I just tripped over UserType's new auto-registration feature, which registers a whole bunch of JodaTime types, but not LocalDateTime. Specifying the type as hinted above is still your only option there. Jul 31, 2012 at 14:19
  • Stefan, you should find LocalDateTime is also registered in the latest release (3.0.0.GA). Oct 25, 2012 at 18:45
30

I'll add this as a separate answer since it's in my opinion important information for anyone upgrading to Hibernate 4, and in need of migrating to using jadira's persistent temporal types. This page is highly ranked in google search results for hibernate 4 and jodatime, so I'll add it here. (For a separate discussion of this issue, see: Joda time DateTime incorrectly stores in database)

If you are in a timezone other than UTC, an important piece of configuration is needed in order to get the same behaviour as you would with a joda-time hibernate-support type. The way jadira's temporal types work by default, is through converting all values to the UTC timezone before persisting to the database, and converting back to the system's timezone when loading the values from the database.

I got burned by this after upgrading, when I had a lot of timestamps with my exact timezone in the database (UTC+1 (+2 when summertime)). When loaded after upgrading to Hibernate 4, 1 or 2 hours (depending on whether or not the timestamp was during summer time) was added to the value in the database, which meant that all existing timestamps were presented erroneously. Further, new timestamp values were stored in the database with UTC timezone, leading them to appear correctly in the application, but wrong in the database. All in all, a hot mess of timezones and timestamps.

So, in order to get the same behaviour as with joda-times hibernate-support (the datetime being persisted being of the timezone of the server in question, and the timestamps in the database being the same as what is being loaded into the application), the following properties must be added to the JPA/Hibernate configuration (in my case, hibernate.properties):

jadira.usertype.autoRegisterUserTypes=true
jadira.usertype.databaseZone=jvm
jadira.usertype.javaZone=jvm

This will make sure that the timestamps in the database will be of the same timezone as that of the application, which in turn will be the timezone of the jvm (in most cases the clock of the application server).

Also, from what I understand, the autoRegisterUserTypes-property removes the need for the @Type-annotation for a selection of common types, among them the Jodatime-types DateTime and LocalDate.

4
  • 2
    Since you have solved your problem, and it was not directly related to this question I will delete my comments and mark yours for deletion, so we keep the comment section clean and to the point.
    – Tobb
    Mar 21, 2014 at 12:37
  • Just a question aside: Why not store every timestamp in UTC time? What if the server moves into a different time zone? Imho it would be best to store all timestamps in UTC time and let the client decide which timezone he is in. Aug 19, 2015 at 17:11
  • As a general rule I would agree. In my case though, there was no chance of the server being moved to a different time zone. Also, the problem here is that the default behaviour changes, in my case without my knowledge. So when you don't know that this happens, you can't migrate your data, and end up with a set of timestamps with different timezones, which is of course not good. But, if it makes sense for your project, you could skip the setup provided here and instead migrate your database. One thing that is negative about going all UTC is that SQL reports get either tricky or wrong.
    – Tobb
    Aug 20, 2015 at 6:09
  • When thinking about it, I do find something fundamentally wrong with storing the dates in another timezone than the server uses. In SQLServer, datetimes are stored as number of days since 1900-01-01, as well as number of clock ticks since midnight. When converting dates to a different timezone, you also change the internal representation of the date, which actually makes it inaccurate. Don't know if this is actually a problem in practice though, but I do know that there are some programs that use the internal representation of the date to calculate the proper timezone offset.
    – Tobb
    Aug 20, 2015 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.