Persist Joda-time's DateTime via Hibernate
Asked Answered
C

4

43

I'm using Jodatime in my Play app, but currently having to do a bunch of converting back and forth from/to java.util.Date and java.sql.Time.

Since jodatime is included in the Play distribution, I'm thinking there's probably a better way to do this. Is there any way I can make my Model fields DateTimes instead of java.util.Date and java.sql.Time so the conversion is done automatically? Is there another way of streamlining this?

Craven answered 8/6, 2011 at 19:26 Comment(0)
I
54

For Hibernate 3 add the following annotation to your date field:

@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")

Hibernate will now do the dirty work for you.

(Make sure you have joda-time-hibernate.jar in your classpath)

UPDATE:

For Hibernate 4 and 5 add the following annotation:

@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")

(Make sure you have jadira-usertype-core.jar in your classpath)

Impel answered 8/6, 2011 at 19:30 Comment(14)
I added that and imported org.hibernate.annotations.Type but got "Could not determine type for: org.joda.time.contrib.hibernate.PersistentLocalDate`". Anything else I need to set up? Or maybe it's a problem with Play only using Hibernate via JPA rather than directly?Craven
You should include the joda-time-hibernate jar in your classpath.Impel
dropped the jar in framework/lib, restarted, and was able to get it working. Their website did trick me at first, the big 'get latest version' link is for joda-time, not joda-time-hibernate.Craven
FYI, the CRUD module doesn't seem to support JodaTime as of now - play.lighthouseapp.com/projects/57987-play-framework/tickets/…Ginkgo
Is there some reason not to use org.joda.time.contrib.hibernate.PersistentDateTime, since that is the data type that was being asked for? If you use this, it's also possible to get the field working with fixtures, since there is a binder for DateTime but not LocalDate.Deodand
Yes that would be a more accurate answer. Thanks for pointing that out. Will edit my answer.Impel
@KeesdeKooter , the question asked for a JPA solution. Your answer uses @Type(...). I guess by this you mean org.hibernate.annotations.Type . Using this annotation creates a dependency on Hibernate. Do you know of a solution that uses the more general JPA instead of Hibernate?Accommodation
@James and Abdull: true. This is a Hibernate only solution. But apparently good enough for the OP to accept the answer. Maybe we should rephrase the question as there is no out of the box solution for JPA as far as I can tell.Impel
@James I corrected the answer in line with the edited question.Impel
If you are in another timezone than UTC, be sure to specify this as a parameter to the @Type-annotation. I just got burned by this, with the times being persisted in the database being 2 hours earlier than they should have been, and appeared in the application.Bara
While this is good for persistence and reading joda datetime fields, it still throws errors when trying to sort the results by that datetime field in JPA named queries. Do you or anyone know of a way around that?Midriff
You should turn to using jadira framework in case your Hibernate version is 4.x.Newsmagazine
Thanks @russellhoff. I added this to the answer.Impel
For anyone trying the above while using Java 7: Jadira usertype-core version 6 is only supported on Java 8 and higher. This results in a rather weird error (some class not found, similar to not having the dependency at all). Therefore you'll have to use verion 5 of the library with Java 7.Raquel
C
10
  1. Joda recommends to use the UserType libraries with Hibernate 4.0, the version of Hibernate bundled with Play 1.2.x (see: http://joda-time.sourceforge.net/contrib/hibernate/index.html).

  2. The proper way to handle the dependency is using the dependencies.yml file, including a line like this:

    - org.jadira.usertype -> usertype.jodatime 2.0.1
    
Christelchristen answered 9/5, 2012 at 8:32 Comment(1)
I would suggest that you open a new question for this.Christelchristen
K
7

Instead of putting the @Type annotation at each of your Joda properties you can add this in your jpa properties

#Hibernate config
jadira.usertype.autoRegisterUserTypes=true

and it should work just fine.

ps. jadira-usertype-core.jar should be in your classpath.

Kweiyang answered 12/6, 2015 at 14:57 Comment(1)
This solution provides less reliance on Hibernate-specifc annotations. Nice!Agronomics
P
0

We should enable two jars in Hibernate 4 project:

  • compile("joda-time:joda-time:2.8")
  • compile("org.jadira.usertype:usertype.jodatime:2.0.1")
Preen answered 28/1, 2015 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.