使用 Hibernate 将 Oracle 日期映射到 Java 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/960923/
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
Mapping an Oracle Date to a Java object using Hibernate
提问by Nathan Spears
I get the message "literal does not match format string".
我收到消息“文字与格式字符串不匹配”。
For instance, here are some methods from a Java class:
例如,这里有一些来自 Java 类的方法:
public String getDateTime();
public void setDateTime(String date_time);
Here is the mapping from the Hibernate config file for that class:
以下是该类的 Hibernate 配置文件的映射:
<property name="dateTime" column="date_time">
and here is the DDL for that column:
这是该列的 DDL:
CREATE TABLE "SCHEMA"."TABLE_NAME"
(
"DATE_TIME" DATE,
etc.
)
I tried setting type="date" and "timestamp" (not at the same time) as an attr on the property in the hibernate config, and then changing the Java type from String to Date, but that gave me a different error. I read something about binding the parameter but couldn't make heads or tails of that.
我尝试将 type="date" 和 "timestamp"(不是同时)设置为 hibernate 配置中属性的属性,然后将 Java 类型从 String 更改为 Date,但这给了我一个不同的错误。我读了一些关于绑定参数的内容,但无法正面或反面。
When I comment out that property from the config everything else is working, so I'm sure that's my issue. The annoying thing is that I have another table/class mapping with seemingly the same Oracle Date->Java String mapping that doesn't give me this problem.
当我从配置中注释掉该属性时,其他一切都在工作,所以我确定这是我的问题。令人讨厌的是,我有另一个表/类映射,它具有看似相同的 Oracle Date->Java String 映射,但不会给我这个问题。
回答by Martlark
You would be better off using a Java.util date class as the property to reflect the oracle date column.
最好使用 Java.util 日期类作为反映 oracle 日期列的属性。
Class blah{
private Date dateTime;
public Date getDateTime();
public void setDateTime(Date dateTime);
blah(){}
}
What was the error when you used Date as the Java class property?
当您使用 Date 作为 Java 类属性时出现了什么错误?
回答by Eugene Ryzhikov
The type of your dateTime attribute should be Timestamp or Date, that should automatically convert Oracle date/time to it. I don't see the point of keeping date as a String since you have to involve the formatting.
您的 dateTime 属性的类型应该是 Timestamp 或 Date,这应该会自动将 Oracle 日期/时间转换为它。我不认为将日期保留为字符串的意义,因为您必须涉及格式。