1

How can I instruct Hibernate to map a Java date to and from an Oracle DATE column using a time zone other than the local time zone?

2 Answers 2

1

I'm not sure but I think that you'll have to use the OracleConnection.setSessionTimeZone(String regionName) method to set the session time zone.

To do that on the OracleConnection before having Hibernate dealing with it, the cleanest way would be to provide and use a custom implementation of o.h.c.ConnectionProvider.

See this related question.

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

9 Comments

Perhaps I'll just normalize the date to the target time zone before handing it over to Hibernate to store in the table.
Pascal, does JDBC translate the JVM time zone to the database session time zone? I just asked this question at stackoverflow.com/questions/4123534/….
Based on the answers to my other question, it seems that time zone translation is specific to the JDBC driver implementation.
@Derek I confess, I don't know what the JDBC spec says about that. But that's where I'd look first. Then, I'll check the conformance of a given JDBC driver.
The JDBC 4.0 Specification (jcp.org/en/jsr/detail?id=221) doesn't even mention time zones! Consequently, JDBC doesn't support SQL type TIMESTAMP WITH TIME ZOME and does not recognize TIMESTAMP literals that include a time zone. So, time zone interpretation must be entirely dependent on JDBC driver implementation!
|
0

Hibernate does not allow for specifying time zones by annotation or any other means. If you use Calendar instead of date, you can implement a workaround using HIbernate property AccessType and implementing the mapping yourself. The more advanced solution is to implement a custom UserType to map your Date or Calendar. Both solutions are explained in this blog post: http://dev-metal.blogspot.com/2010/11/mapping-dates-and-time-zones-with.html

1 Comment

Joobik, in your article, you claim that Hibernate translates the time zone of the given Date: "So what Hibernate does, is using the time zone schedule of its JVM (which runs in CET) to convert the time zone independent java.util.Date to the time zone specific date string in our DB record. Because CET is one hour ahead of GMT, one hour has been added to our DB date." However, according to stackoverflow.com/questions/4123534/…, it's the JDBC driver that translates the time zone, not Hibernate.

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.