Question

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?

Était-ce utile?

La 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:

  • Many of the mapping classes listed in the Javadoc I linked have 'local' equivalents; e.g. PersistentLocalDateTime for PersistentDateTime.

  • 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:

  • The MySQL server (and all MySQL instances on our dev machines) have been configured to use the UTC time zone
  • We are using PersistentDateTime as opposed to PersistentLocalDateTime in the Hibernate config files
  • jadira.usertype.databaseZone & jadira.usertype.javaZone are both set to UTC

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top