mysqlhibernatejodatimeusertype

How to integrate Joda-Time and Hibernate 4/MySQL 5?


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?


Solution

  • 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 the org.jadira.usertype.dateandtime.joda.PersistentDateTime class as the 'type'. For example:

    <class name="DateTestEntity" table="DATE_TEST">
        <id name="id" column="ID">
            <generator class="native" />
        </id>
    
        ...
    
        <property name="dateOfBirth" type="org.jadira.usertype.dateandtime.joda.PersistentDateTime" column="DATE_OF_BIRTH" />
    </class>
    

    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:

    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:

    If 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.