Reputation: 339
I have an application that is using hibernate mappings to perform a select over multiple DATETIME columns in a SQL Server database table. The hibernate mapping takes this DATETIME and transforms it to a java.util.Date object in the application.
Database table
Start_time | datetime
10-OCT-2014 06:45:00
10-OCT-2014 13:30:00
The problem is that the times that are stored in the DATETIME fields in the database are being dropped/truncated and every date is returned with a time of 00:00 when hibernate is bringing them into the application:
Hibernate Column mapping:
.
.
<property name="start_dt" type="java.util.Date">
<column name="Start_time"/>
</property>
.
I have it mapping to a date object with the proper getters/setters and my query execution looks like this:
Query query = session.createSQLQuery("SELECT Start_time FROM table")
.addScalar("Start_time", Hibernate.Date)
.setResultTransformer(Transformers.aliasToBean(Class.class);
I am getting this:
List<Date> = { '10/10/2014 00:00' , '10/10/2014 00:00' }
What I want is this:
List<Date> = { '10/10/2014 06:45:00' , '10/10/2014 13:30:00' }
The application is doing something similar with an Oracle connection and it is returning the times successfully. The only variable is that my connection is to a SQL Server database. Does anyone know how to prevent the times from being dropped?
UPDATE:
Based on the selected answer bleow and because the comment isn't showing correctly the fix for this is the following:
Query query = session.createSQLQuery("SELECT Start_time FROM table")
.addScalar("Start_time", Hibernate.TIMESTAMP)
.setResultTransformer(Transformers.aliasToBean(Class.class);
Upvotes: 3
Views: 3330
Reputation: 8797
Try to use timestamp
<property name="start_dt" type="timestamp">
<column name="Start_time"/>
</property>
ANSI SQL DATE doesn't contain time
Mapping type: date
Java type: java.util.Date or java.sql.Date
ANSI SQL Type:DATE
Mapping type: timestamp
Java type: java.util.Date or java.sql.Timestamp
ANSI SQL Type: TIMESTAMP
Upvotes: 3