I have been wanting to move to Joda-Time for our application for awhile. There are a few hangups...
1-The offical Joda-Time integration here seems to be out of date for Hibernate 4.x applications
2-The suggested alternative here seems to have zero documentation/getting started info.
3-I cannot find a good reference for which types in my database (MySql 5.5.11) map nicely to the Joda-Time equivalents.
Does anybody have any idea how to deal with these?
I have had some success using the Jadira UserType library that you linked to. In your hibernate mapping configuration you can map Joda
DateTime
properties to MySQL DATETIME or TIMESTAMP columns by specifying theorg.jadira.usertype.dateandtime.joda.PersistentDateTime
class as the 'type'. For example:I am not sure about mappings between other MySQL and Joda-Time types, but these Javadocs list the classes available to use in your mapping configuration.
This is all you need to do to get started. However, there are a few things you need to consider regarding time-zones:
Many of the mapping classes listed in the Javadoc I linked have 'local' equivalents; e.g.
PersistentLocalDateTime
forPersistentDateTime
.There are two properties that UserType uses when considering time-zones:
jadira.usertype.databaseZone
&jadira.usertype.javaZone
. (Have a look at this question or this question to see where they can be set.) I don't know what the differences between them are, but databaseZone seems to be the most important one to look at as this question seems to indicate.In the project I am working on we want to store all dates in UTC (i.e. GMT) and only change them to specific timezones when displaying them to the user. We have had no problems so far with the following set-up:
PersistentDateTime
as opposed toPersistentLocalDateTime
in the Hibernate config filesjadira.usertype.databaseZone
&jadira.usertype.javaZone
are both set to UTCIf your application needs to handle international dates then I suggest that you do the same, or else experiment with the above settings until you are satisfied that your application behaves correctly.