-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Calendar persisting doesn't save timezone?
PostPosted: Sat Jan 08, 2005 7:53 pm 
Newbie

Joined: Wed Aug 04, 2004 4:16 pm
Posts: 12
Location: UCSC
Hi,
I would like to understand why the built-in Hibernate type "calendar"
does not persist the timezone along with the date and time. I can
understand why Locale isn't saved, but the timezone seems like a
requirement if a query wants the results ordered by the calendar
timestamp. Specifically, 2AM GMT is before 5AM PST for any given
day, but these would be ordered with 2AM before 5AM. This situation
should only come up if Hibernate is writing to the same database from
two different computers in different timezones, as I understand it.
The source code uses a SimpleDateFormat to get the timestamp string
to write to the DB, which assumes local timezone. So:
Computer 1, EST processes data from "2AM GMT to 8AM GMT" and
Computer 2, PST processes data from "8AM GMT to 2PM GMT", and
each writes their results to a shared DB. The data includes a persisted
Calendar that has the GMT timezone set and the timestamp of the
processed data. Computer 1's data will be saved in a timestamp range
from 9pm (the previous day) to 5am, and Computer 2's data will be
saved in a timestamp range from 12am to 6am. Needless to say, the
data will now be interleaved, according to the timestamp data, when
the original data was in two separate batches.

So...Yes, I can certainly make a subclass of Calendar and persist
it in such a way that my retrieved Calendar has the correct timezone
set *and* that the database timestamps are sortable, but I'm wondering
if this is an issue that is being discussed or is otherwise already known.
I didn't manage to come up with a search string that gave me any
relevent results in my Forum search...

Thanks,
-Jennifer Bevan


Hibernate version:
2.1.6

Mapping documents:
java.util.Calendar cal;
/**
@hibernate.property type="calendar"
*/

Name and version of the database you are using:
MySQL 4.1.X


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 7:32 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
What type of field are you persisting the Calendar to? Probably a DateTime, Date, or Timestamp, depending on your DB. Keep in mind that once the value is persisted to the DB server, the timezone information is converted to the timezone of the DB server. Date/Time fields in the DB server do not have a concept of timezone. The database machine itself has a timezone, but that's usually it. If you store it as a VARCHAR you might get different results.

One possible solution would be to use your own UserType that is smart about how it sets dates when the value is read out of the database. I've found that when I can't get Hibernate to do something the way I need it to, a custom UserType is often the ticket. If you really need to get aggresive and store data in two column fields, (such as a DATETIME and a TIMEZONE column) that is then translated into a single Calendar, you can use a CompositeUserType that translates everything for you. I've done things like that (and wierder!) and it's worked wonderfully.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 2:04 am 
Newbie

Joined: Wed Aug 04, 2004 4:16 pm
Posts: 12
Location: UCSC
Actually, I was talking about the Hibernate built-in type for persisting a Calendar, which
I think is "type = calendar", which has the corresponding SQL type of TIMESTAMP.
So, yes, I understand that this mapping won't persist a timezone, and yes, I will most
likely just persist milliseconds since Jan 1, 1971 GMT (or whatever getMillis gives me),
but my question is mainly about the *appropriateness* of mapping Calendar to a
TIMESTAMP, because you can't get an equivalent Calendar back out of the DB; any
Calendar that it recreates is *not* guaranteed to have the timezone that it had when
it was originally stored in the DB. So while it's not actually *misleading* (because the
book does say "TIMESTAMP" on page 199, it just seems...incomplete. So that's why
I'm asking the question.

Thanks for the comments, though!
-Jennifer Bevan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 7:37 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Use SQL "TIMESTAMP WITH TIME ZONE" type to store time zone information with time.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 12:09 pm 
Newbie

Joined: Wed Aug 04, 2004 4:16 pm
Posts: 12
Location: UCSC
Ah! I"m learning SQL at the same time I'm learning Hibernate, so these nice SQL
types are handy. So...okay, I have enough to come up with a workaround. Perhaps at some point
in the future Hibernate could change the built-in-type "calendar" to use the
TIMESTAMP WITH TIME ZONE sql type?

Thanks all,
Jennifer Bevan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 12:18 pm 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
if you don't want to wait, you could write your own Calendar-UserType which will use the TIMESTAMP WITH TIME ZONE sql type ...

gtx
curio


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 12:44 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can also write a custom Dialect


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.