将 Oracle Date 与 Hibernate 结合使用时的日期/时间的 Java 类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2188768/
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-08-13 04:33:43  来源:igfitidea点击:

Java type for date/time when using Oracle Date with Hibernate

javaoraclehibernatedatetimeorm

提问by Marcus Leon

We have a Oracle Date column. At first in our Java/Hibernate class we were using java.sql.Date. This worked but it didn't seem to store any time information in the database when we save so I changed the Java data type to Timestamp. Now we get this error:

我们有一个 Oracle 日期列。起初,在我们的 Java/Hibernate 类中,我们使用java.sql.Date. 这有效,但当我们保存时,它似乎没有在数据库中存储任何时间信息,所以我将 Java 数据类型更改为时间戳。现在我们得到这个错误:

springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.an notation.PersistenceExceptionTranslationPostProcessor#0' defined in class path resource [margin-service-domain -config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreatio nException: Error creating bean with name 'sessionFactory' defined in class path resource [m-service-doma in-config.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Wrong column type: CREATE_TS, expected: timestamp

springframework.beans.factory.BeanCreationException:创建名为'org.springframework.dao.an notation.PersistenceExceptionTranslationPostProcessor#0'的bean在类路径资源[margin-service-domain -config.xml]中定义时出错:bean初始化失败;嵌套异常是 org.springframework.beans.factory.BeanCreatio nException:在类路径资源 [m-service-doma in-config.xml] 中定义名称为“sessionFactory”的 bean 创建时出错:init 方法调用失败;嵌套异常是 org.hibernate.HibernateException:错误的列类型:CREATE_TS,预期:时间戳

Any ideas on how to map an Oracle Datewhile retaining the time portion?

关于如何Date在保留时间部分的同时映射 Oracle 的任何想法?



Update: I can get it to work if I use the Oracle Timestampdata type but I don't want that level of precision ideally. Just want the basic Oracle Date.

更新:如果我使用 OracleTimestamp数据类型,我可以让它工作,但我不希望理想的精度水平。只想要基本的 Oracle Date

采纳答案by Pascal Thivent

Not a direct answer but I'd use the Oracle TIMESTAMP type:

不是直接的答案,但我会使用 Oracle TIMESTAMP 类型:

  • TIMESTAMP (fractional_seconds_ precision)Year, month, and day values of date, as well as hour, minute, and second values of time, where fractional_seconds_precisionoptionally specifies the number of digits in the fractional part of the SECONDdatetime field and can be a number in the range 0 to 9. The default is 6. For example, you specify TIMESTAMPas a literal as follows:

    TIMESTAMP'1997-01-31 09:26:50.124'
    
  • TIMESTAMP (fractional_seconds_precision) 日期的年、月和日值,以及时间的小时、分钟和秒值,其中 fractional_seconds_precision可选地指定SECOND日期时间字段的小数部分中的位数, 并且可以是范围 0 到 9。默认值为 6。例如,您指定 TIMESTAMP为文字如下:

    TIMESTAMP'1997-01-31 09:26:50.124'
    

with the desired fractional_second_precision.

与所需的fractional_second_precision.

回答by Drew Wills

Did you recreate the database after you made this change?

进行此更改后是否重新创建了数据库?

If Hibernate originally created the table using a java.sql.Date for the column, I'm not aware of any way it could change it based on a change in the Java code alone.

如果 Hibernate 最初使用列的 java.sql.Date 创建表,我不知道它可以根据 Java 代码的更改以任何方式更改它。

回答by Andrzej Doyle

First off - you're right that you'd need to use a java.sql.Timestamp class, as the java.sql.Date class explicitly does not represent a specific time of day (rather, it tries to represent midnight GMT).

首先 - 你是对的,你需要使用 java.sql.Timestamp 类,因为 java.sql.Date 类明确不代表一天中的特定时间(相反,它试图代表格林威治标准时间午夜)。

As for your error - you haven't given enough information to do anything more than guess (it would require looking at both your Hibernate config and the class to actually determine the cause). However, if you merely changed the class of the field in the Java class, then of course you'll have to update the Hibernate mapping as well. If you haven't done the latter then this will likely lead to your mismatch. Try explicitly specifying type="timestamp"for the corresponding mapping.

至于你的错误 - 你没有提供足够的信息来做更多的猜测(它需要查看你的 Hibernate 配置和类来实际确定原因)。 但是,如果您只是在 Java 类中更改字段的类,那么您当然也必须更新 Hibernate 映射。如果您还没有完成后者,那么这可能会导致您的不匹配。尝试明确指定type="timestamp"相应的映射。

EDIT: If you're using annotations, then did you update the annotation on that property to @Temporal(TemporalType.TIMESTAMP)? If you didn't, then you will need to (and if you did, you should have said so :-)).

编辑:如果您正在使用注释,那么您是否将该属性上的注释更新为@Temporal(TemporalType.TIMESTAMP)?如果你没有,那么你将需要(如果你这样做了,你应该这么说:-))。

回答by Brian Deterling

I always use java.util.Date with Oracle dates and it handles date and time just fine.

我总是将 java.util.Date 与 Oracle 日期一起使用,它可以很好地处理日期和时间。

回答by wallenborn

Maybe you have this problem?Make sure you have the latest JDBC driver, the filename should be ojdbc5.jar.

也许你有这个问题?确保您拥有最新的 JDBC 驱动程序,文件名应为 ojdbc5.jar。

回答by sachin farande

You need to use @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")for the column defined in entity class

您需要对 实体类中定义的列使用 @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")