oracle 带时区的休眠时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1289492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 18:48:24  来源:igfitidea点击:

Hibernate Timestamp with Timezone

oraclehibernatetimetimezonetimestamp

提问by joobik

I'm new to Hibernate and am working with an Oracle 10g database. We have columns in our tables that are of type TIMESTAMP WITH TIMEZONE. Hibernate does not seem to support this mapping directly. Is there a standard way to go about this?

我是 Hibernate 的新手,正在使用 Oracle 10g 数据库。我们的表中有类型为 的列TIMESTAMP WITH TIMEZONE。Hibernate 似乎不直接支持这种映射。有没有标准的方法来解决这个问题?

回答by joobik

An example of a UserType storing java.util.Calendar with time zone information is given in this blog post: http://www.joobik.com/2010/12/mapping-dates-and-time-zones-with.html

在此博客文章中给出了存储 java.util.Calendar 和时区信息的 UserType 示例:http: //www.joobik.com/2010/12/mapping-dates-and-time-zones-with.html

回答by ChssPly76

TIMESTAMP WITH TIMEZONEis Oracle extension and thus is not supported by Hibernate out of the box. You have two options:

TIMESTAMP WITH TIMEZONE是 Oracle 扩展,因此开箱即用的 Hibernate 不支持。您有两个选择:

1) Change your table structure to store timezone in a separate column (as VARCHAR2). Hibernate is able to map java.util.TimeZoneas timezonetype using its ID.

1) 更改表结构以将时区存储在单独的列中(如 VARCHAR2)。Hibernate 能够使用其 ID将 java.util.TimeZone 映射timezone类型。

2) Write a custom class to hold both timestamp and timezone and a custom UserTypethat would persist it. It will have to be a CompositeUserTypeif you need the ability to use its individual properties (e.g. timezone or timestamp) in queries. Look at this exampleto get you started; you'll need to alter it to actually store the timezone.

2) 编写一个自定义类来保存时间戳和时区以及一个可以持久化它的自定义UserType。如果您需要能够在查询中使用其单独的属性(例如时区或时间戳),则它必须是CompositeUserType。看看这个例子让你开始;您需要更改它以实际存储时区。