2

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

tjholmes66
  • 1,850
  • 4
  • 37
  • 73
  • Thankyou for advising about the two blank pages (Howto and Bugs)... actually both can be removed, as bugs are tracked through jadira.atlassian.net, and howto is covered by the User Guide page. The Jadira docs have the same coverage as Joda Time contrib but do not list all the types. You can select your types by looking at the Javadoc - they are all prefixed with 'Persistent', I don't list them because there are about 40 and growing. Disclaimer: I am the maintainer of Jadira Usertype. – Chris Pheby Aug 15 '13 at 16:02
  • I assume you meant you converted joda.PERSISTENTDateTime to jadira.PersistentDateTime? As to your maths functions, can you give an example of what they are, without that it is difficult to suggest how to resolve. – Chris Pheby Aug 15 '13 at 16:05

2 Answers2

3

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.

LaurentG
  • 11,128
  • 9
  • 51
  • 66
  • Thanks for the info, but I am the one making the architectual decisions for this project, and they trust my decisions. The piece of code you added here I am familiar with, and it's the extent of the documentation from the user guide. Have you seen the user guide? I saw it several times before I posted this message, and it was lots of blank pages, so that is not very helpful. – tjholmes66 Aug 10 '13 at 13:13
  • To restate: when the system is using Hibernate 3, they were using the Joda-Time-Hibernate to get dates from the database. Next ... there is lots and lots of date math using the joda-time library, and with the joda-time dates that worked great. When I change one entity to use Jadira PersistentDateTime, not all my date math functions do not work, and that is a lot of code-writing. How This is the most important question I need answering. – tjholmes66 Aug 10 '13 at 13:21
  • 1
    Btw, in Hibernate 4 there is an easier way to do this. You can define that org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime be used as the default Hibernate Type for all attributes defined as LocalDateTime. See http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html_single/#types-registry. Given the proper registration, you'd just define the attribute as `private LocalDateTime updated;`.. no annotations needed – Steve Ebersole Aug 25 '13 at 18:25
  • Also, in Hibernate 4 (this is being improved for Hibernate 5) jadira could handle registering all those Type registrations for you. See org.hibernate.metamodel.spi.TypeContributor (which will move more than likely for 5.0) – Steve Ebersole Aug 25 '13 at 18:28
3

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!

tjholmes66
  • 1,850
  • 4
  • 37
  • 73