If you need to compare dates for equality in an HQL SELECT statement, but the hh:mm:ss portion of the database date makes equality fail, there is a simple and elegant solution, courtesy of Hibernate mapping 'formula' attribute.
Here's an example for Oracle:
<property name="myDateTime" type="date" column="PRO_CRD" />
<property name="myDate" type="date" formula="trunc(PRO_CRD)"/>
The date property, 'myDateTime', is mapped as usual. The trick is to add a 'computed' property that applies a TRUNCATE function on myDateTime. The computed property does not need a column in the table, however it needs a JavaBean-type java attribute in the POJO. Also, you must cleanup your java Date before you execute the query (removing hh:mm:ss is very easy with DateFormatter).
So, your HQL query would simply include something like this:
" and product.myDate = :myJavaDateName "
As a result, you avoid all query string manipulation, which is tedious, and vulnerable to SQL-injection attacks.
Hope this helps!
|