oracle Java Date Hibernate 截止时间

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

Java Date Hibernate cut off time

javaoraclehibernatedatetime

提问by Vlad

I have a Date type column in Oracle DB and it contains date and time for sure. But when I'm trying to get data in java application it will return date with bunch of zeros instead of real time. In code it'll be like:

我在 Oracle DB 中有一个日期类型列,它肯定包含日期和时间。但是当我尝试在 Java 应用程序中获取数据时,它将返回带有一堆零的日期而不是实时。在代码中它会像:

SQLQuery sqlQuery = session.createSQLQuery("SELECT table.id, table.date FROM table");
List<Object[]> resultArray = sqlQuery.list();
Date date = (Date)resultArray[1];

If it was 26-feb-2010 17:59:16 in DB I'll get 26-feb-2010 00:00:00 How to get it with time?

如果是 26-feb-2010 17:59:16 在 DB 我会得到 26-feb-2010 00:00:00 如何随着时间得到它?

采纳答案by Alexander Pogrebnyak

I am not an expert with Oracle, but you probably need a DateTimecolumn type to get a time stamp.

我不是 Oracle 专家,但您可能需要一个DateTime列类型来获取时间戳。

With that you need to use java.sql.TimestampJDBC type.

有了它,您需要使用java.sql.TimestampJDBC 类型。

回答by Aerthel

As others have already stated, you need to use java.sql.Timestampto get the time.

正如其他人已经说过的那样,您需要使用java.sql.Timestamp来获得时间。

However, if you use the @Temporal(TemporalType.TIMESTAMP)annotation on a Oracle's DATE column, Hibernate will complain and throw schema validation errors. To avoid those, add a columnDefinitionlike this:

但是,如果您@Temporal(TemporalType.TIMESTAMP)在 Oracle 的 DATE 列上使用注释,Hibernate 会抱怨并抛出模式验证错误。为了避免这些,添加一个columnDefinition这样的:

@Temporal(TemporalType.TIMESTAMP)
@Column(name="EVENTTIME", columnDefinition="DATE")
private Date eventTime;

回答by hansfbaier

I had the same problem (Oracle Date type in the database).

我遇到了同样的问题(数据库中的 Oracle 日期类型)。

The following mapping works for me:

以下映射对我有用:

<property name="timeStamp" type="java.util.Date">
    <column name="TIME_STAMP"/>
</property>

回答by Robert Durgin

I've encountered this issue in the past too. To resolve it, change your hibernate mapping from:

我过去也遇到过这个问题。要解决它,请从以下位置更改您的休眠映射:

<property name="timeStamp" type="java.util.Date">
    <column name="TIME_STAMP"/>
</property>

to

<property name="timeStamp" type="timestamp">
    <column name="TIME_STAMP"/>
</property>

You can leave the data type in your Hibernate object as java.util.Date.

您可以将 Hibernate 对象中的数据类型保留为 java.util.Date。

回答by prashant thakre

Yeah it also working fine hibernate 3.3 , use ojdbc6.jar and in annotation change the date to timestamp like below

是的,它也可以正常运行 hibernate 3.3,使用 ojdbc6.jar 并在注释中将日期更改为时间戳,如下所示

@Id
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "TDATE", length = 7)
public Date getTdate() {
    return this.tdate;
}

public void setTdate(Date tdate) {
    this.tdate = tdate;
}

回答by Rakesh Gourineni

I was into the same problem recently. Here is my approach...

我最近遇到了同样的问题。这是我的方法...

So in this case define a for loopin the QueryImplementation class ,such that it checks the type of all columns of the database table. If the column name is of type DATE then append addScalar("columnName", new Timestamptype()) to the query builder. In this way the Date type Columns will display the TimeStamp in the java application.

所以在这种情况下,在 QueryImplementation 类中定义一个for 循环,以便它检查数据库表的所有列的类型。如果列名称是 DATE 类型,则将 addScalar("columnName", new Timestamptype()) 附加到查询构建器。通过这种方式,日期类型列将在 java 应用程序中显示时间戳。

Please find the sample code below:

请在下面找到示例代码:

 SQLQuery hibernateQuery = getSession().createSQLQuery(sqlQuery);

        Set<String> columns = columnDataTypes.keySet();
   for (String column : columns) {
       if (columnDataTypes.get(column).equals(SqlType.DATE.name())) {
           hibernateQuery.addScalar(column, new TimestampType());
       } else {
           hibernateQuery.addScalar(column);
       }
   }

Hope this helps.

希望这可以帮助。

Rakesh.

拉克什。

回答by Ghostwritertje

Maybe a little late, but in my case the solution was to just add @Temporal(TemporalType.DATE) to my date-field.

也许有点晚了,但就我而言,解决方案是将 @Temporal(TemporalType.DATE) 添加到我的日期字段。

@Temporal(TemporalType.DATE)
private Date date;

And another solution we found (when the first one didn't work) was to explicitly tell hibernate the Type:

我们发现的另一个解决方案(当第一个不起作用时)是明确告诉 hibernate 类型:

@Type(type = "date")
private Date date;

回答by Brian Deterling

Try this:

尝试这个:

session.createSQLQuery("SELECT table.id as i, table.date as d FROM table")
  .addScalar("i", Hibernate.LONG)  // Or whatever type id is
  .addScalar("d", Hibernate.TIMESTAMP);

回答by wallenborn

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

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