2

I'm fairly new to Hibernate. I want to map some object, let's say String to a mysql date (which is a date without time or timezone). I thought this would be fairly straight forward. But I can't seem to find a good solution.

In my actual app, I'm not using a String. I want to use something like LocalDate in JSR 310 or my own simple month-date-year triplet type.

I really... really don't want to write some internal setter or getter which translates my simple object into a java.sql.Date or java.util.Date because:

  1. It bloats the domain object with unnecessary code.
  2. I'd have to write the same code in all my domain objects
  3. It sort of defeats the whole purpose of me using this type of class. The whole point of using these classes is to avoid any possible timezone issues. It seems really stupid to introduce a class (java.sql.Date or java.util.Date) which has time and timezone when I only want to persist a simple date (like a birthday date - October 1, 2005).

I suppose the underlying problem is the JDBC driver might only be able to map the core java Date classes to the mysql date types???

2 Answers 2

2

May I recommend you look at an already existing solution and see if you can't either:

  1. Use it in your own Code
  2. Use it as an example to inspire your code

http://joda-time.sourceforge.net/contrib/hibernate/index.html

I personally recommend Joda Time for your time/date needs, and there's already a Hibernate bridge for it. Barring your use of Joda Time, take a look at the URL above, and the code for the hibernate bridge as instruction for how you should be writing a converter.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I checked out the source code for that project. It looks fairly simple to write my own, but maybe I'll just stick with JODA for now.
1

Since you have String for your date columns I guess there is no way out except adding helper methods to convert. However when timezones are involved its best to pass dates in millis (long) which can easily be use to create Date objects. Timezone can be then applied as required. So use the standard Date classes which has mappings with mysql date types.

4 Comments

Passing in a millis since epoch is exactly opposite of what I want to do. That represents a specific instance in time. Also, I ideally won't use String. I'll use JSR 310's LocalDate or JODA-time equivalent which represents a date without time or timezone.
I'll give you an example of why I don't want to pass in a Date. I get an instance to Calendar, calendar.setTimeZone("Asia/Shanghai"), calendar.clear(), calendar.set(2011, Calendar.JULY, 23), myDomainObj.setDate(new java.sql.Date(calendar.getTimeInMillis())). It looks innocent. I'm creating the proper date in the calendar. When I wrap it in the java.sql.Date it now represents July 22. Sure this is a bug, but why would I even want the "chance" of having bugs like this. My simple date should have nothing to do with timezones.
Its creating the Date object in default Timezone that corresponds to GMT. So when you get it back, you apply the appropriate timezone. Here is a code snippet. Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(TimeZone.getTimeZone(("Asia/Shanghai"))); calendar.clear(); calendar.set(2011, Calendar.JULY, 23); java.sql.Date dt = new java.sql.Date(calendar.getTimeInMillis()); System.out.println (dt); Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone(("Asia/Shanghai"))); cal1.setTime(dt); System.out.println ( " Calendar " + cal1.get(Calendar.DATE));
Yeah I understand that java.sql.Date's getters use the jvm timezone which causes this problem. But are you suggesting to persist the time as July 22 in the DB and then add the timezone correction after you get it out of the database? If so, I'll have to strongly disagree. What happens if you move your DB server to europe. Now your entire database is garbage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.