Vlad
Vlad

Reputation: 117

Java Date Hibernate cut off time

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?

Upvotes: 11

Views: 18707

Answers (9)

Ghostwritertje
Ghostwritertje

Reputation: 31

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;

Upvotes: 0

Rakesh Gourineni
Rakesh Gourineni

Reputation: 1421

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.

Upvotes: 0

Aerthel
Aerthel

Reputation: 659

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;

Upvotes: 11

prashant thakre
prashant thakre

Reputation: 5147

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

Upvotes: 2

Robert Durgin
Robert Durgin

Reputation: 1840

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.

Upvotes: 2

hansfbaier
hansfbaier

Reputation: 639

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>

Upvotes: 3

wallenborn
wallenborn

Reputation: 4273

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

Upvotes: 0

Brian Deterling
Brian Deterling

Reputation: 13724

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

Upvotes: 0

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45576

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.

Upvotes: 4

Related Questions