Hibernate 4 and joda-time

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 consider the current latest version.

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

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

Do they have any known issues using these versions together?

Update Well, it turns out that the columns are created, but cannot be filled with any data:

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

They are incompatible, and I have to use usertype . See answer below.

+62
java hibernate jodatime
Jan 23 '12 at 16:11
source share
2 answers

A clear lack of documentation means that it may be useful for me to write down the steps required for integration. Make sure your libraries are up to date.

You will need: [if you already have hibernate4]

Latest joda-time version

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

and user type lib

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

Then use the following entity classes (optional for LocalDateTime, there may be any of the available classes):

 import org.joda.time.LocalDateTime; 

and to define a column:

 @Column(name="updated", nullable = false) @Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime") private LocalDateTime updated; 
+96
Jan 24 2018-12-12T00:
source share

I will add this as a separate answer, because in my opinion, this is important information for those who are upgrading to Hibernate 4 and need to switch to the use of constant jadira temporary types. This page is highly rated in google search results for hibernate 4 and jodatime, so I will add it here. (For a separate discussion of this issue, see: Joda DateTime is not stored correctly in the database. )

If you are in a time zone other than UTC, an important configuration is needed to get the same behavior as with the hibernate support type for joda time. The way jadira temporary types work by default is to convert all values โ€‹โ€‹to UTC before saving the database and converting back to the system time zone when loading values โ€‹โ€‹from the database.

I was burned by this after the update, when I had a lot of timestamps with my exact time zone in the database (UTC + 1 (+2 in summer)). When downloading after switching to Hibernate for 4, 1 or 2 hours (depending on whether there was a timestamp in the summer), a value is added to the database, which means that all existing timestamps were presented incorrectly. In addition, the new timestamp values โ€‹โ€‹were stored in the database with the UTC time zone, which led to their correct display in the application, but was incorrect in the database. All in all, a hot mess of timeshares and timestamps.

So, in order to get the same behavior as with hibernate support for joda-time (the datetime is stored in the time zone of the server in question, and the timestamps in the database match what is loaded in the application), the following properties should be added to JPA / Hibernate configuration (in my case hibernate.properties ):

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

This ensures that the timestamps in the database will have the same time zone as the application, which in turn will be the jvm time zone (in most cases, the application server clock).

Also, from what I understand, autoRegisterUserTypes -property eliminates the need for @Type -nnotation to select common types, including the Jodatime types DateTime and LocalDate .

+29
Aug 21 '13 at 12:43 on
source share



All Articles