-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: HQL date equality comparison: a solution
PostPosted: Thu Apr 24, 2008 2:18 am 
Newbie

Joined: Tue Nov 30, 2004 5:35 am
Posts: 5
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!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 24, 2008 9:41 pm 
Newbie

Joined: Tue Nov 30, 2004 5:35 am
Posts: 5
In fact there's a much more straightforward solution:

" and trunc(product.myDateTime) = :myJavaDateName "

Just use a TRUNCATE function on the fly on the regular Date property. This will work as long as you also 'truncate' the java date (use DateFormat for that).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.