Java Date Hibernate cut off time

18,139

Solution 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.

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

Solution 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>

Solution 4

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

Solution 5

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.

Share:
18,139
Vlad
Author by

Vlad

Updated on June 04, 2022

Comments

  • Vlad
    Vlad almost 2 years

    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?