views:

614

answers:

2

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
In the mapping type?
Abhi
"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
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
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
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
With JPA, I think you're supposed to use `@Temporal(DATE)`
Pascal Thivent
@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