Java Date Hibernate cut off time

…衆ロ難τιáo~ 提交于 2019-12-09 14:54:31

问题


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:

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?


回答1:


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

With that you need to use java.sql.Timestamp JDBC type.




回答2:


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

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 columnDefinition like this:

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



回答3:


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

The following mapping works for me:

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



回答4:


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.




回答5:


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

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

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



回答6:


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);



回答7:


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




回答8:


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

So in this case define a for loop in 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.

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.




回答9:


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

@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:

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


来源:https://stackoverflow.com/questions/2343339/java-date-hibernate-cut-off-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!