I have a MySQL table with one of the column type as Date. In my hibernate mapping file I have mapped this column to type java.util.Date. Now in HQL while trying to retrieve objects based on date equality I do not get any results if I set the Date using new Date(). If I normalize the date by setting hours, minutes and seconds to zero I get results. Is this required since I have declared the SQL column type to be a Date and not Timestamp?
+2
A:
Try java.sql.Date
Hopefully this will help you out with the date equality issue.
RDJ
Richie
2009-11-30 11:07:07
In the mapping type?
Abhi
2009-11-30 11:10:15
"I have a MySQL table with one of the column type as Date. In my hibernate mapping file I have mapped this column to type java.util.Date" - instead try java.sql.Date
Richie
2009-11-30 11:58:45
Thanks. That works but, do you have any idea why it does not work with the type java.util.date even when the column type is Date? If it is Timestamp I understand, but with Date this thing baffles me.
Abhi
2009-11-30 14:30:20
it cud b probably becoz date format in database (sql) is different from that of date in util(date in util). Noramally for mapping dates in database to hibernate java.sql.Date is used. Happy to help :)
Richie
2009-12-01 04:33:04
A:
I was able to use java.util.Date with a SQL Server Date column by explicitly setting the @Type:
@Type(type="date")
public java.util.Date getDate() {
return date;
}
Without the annotation, I was getting
java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
with Hibernate 3.5.1-final
Andrew
2010-05-13 15:33:57
@Temporal(TemporalType.DATE) also works for me. There's some documentation on it here: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping-property that makes it look like that's more standard - thanks, Pascal!
Andrew
2010-05-13 22:23:17