Question

I was handed a legacy Spring Hibernate DAO project that was Spring 3.2.0 and Hibernate 3.5. I converted it upstream to Spring 3.2.3.RELEASE, and Hibernate 4.2.3.Final.

The problem is that they used Joda-Time for peristing date/times to the database, but from what I have read here, joda-time-hibernate 1.3 does not work for Hibernate 4, but only for 3. So, I looked at the recommended libraries and spent a lot of time converting Joda.DateTime to jadira.PersistentDateTime, but what I found was that Jadira ... IMHO doesn't work that great for my needs. Yes, I can pull objects from the database, and these are Jadira PersistentDateTime fields, this works great.

However, we have lots and lots and lots of date calculations used with joda-time, one for adding days, emoving days, difference between days, before, after calculations, and the list goes on.

So ... I am wondering ... can I convert a joda.DateTime to and from jadira.PersistentDateTime???? I also find the documentation for Jadira non-existent, I find almost no good documentation out there. If someone can point me out to this documentation that would be great.

On the other hand, since I am using Hibernate 4 now, I see no problem with just using a standard java.util.Date for my persistent objects.

Should I stick to jadira, is it worth it? Or should I use the standard reliable java.util.Date with the GregorianCalendar? I am leaning toward the latter since it should be more stable and reliable that some exotic third-party package.

Thanks! Tom

Was it helpful?

Solution 2

I used java.util.Date in my entities and nothing bad happens. I can persist dates and times to and from the database with no issues using Hibernate 4 using the latest jars. Examples all over the internet show the date as java.util.Date without having to use Jadira.

In the existing code in my applications, I just had to make the appropriate changes as they came up: converting from java.util.Date to org.joda.time.DateTime and back again.

The code from the Joda-Time User Guide has a good example of doing this:

  // from Joda to JDK
  DateTime dt = new DateTime();
  Date jdkDate = dt.toDate();

  // from JDK to Joda
  dt = new DateTime(jdkDate);

So, my solutions was to just not use Jadira at all. Hope this helps!

OTHER TIPS

Your colleagues would be probably very unhappy if you go back to java.util.Date! :-)

Jadira works great with Hibernate 4. I think you have to understand how does it work. Jadira adds user type mapping for Hibernate in order to allow Hibernate persist directly other types as the standard (as String, Integer, Boolean, etc.).

You would typically do something like this:

@Column
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime updated;

See more information in the user guide.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top