-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to compare date with HQL
PostPosted: Sat Feb 23, 2008 4:06 pm 
Newbie

Joined: Sat Feb 23, 2008 1:18 pm
Posts: 2
Hello all,

Hibernate version: 3
DB: MySQL5

I need to compare date entered by the user in format yyyy/mm/dd with the date in database.

e.g MY HQL is
select cust.customerName
from customer cust
where cust.expiryDate = '2009/02/21'

This returns me 0 records, as in MySQL cust.expiryDate is '2009-02-21 14:27:02.0'. My problem is I do not know how to convert cust.expiryDate date in user entered date format. With MySQL native statement that would be easy, but I do not know how it works in HQL.

Would anybody help me here?

Regards
intecgmbh


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 24, 2008 1:17 pm 
Newbie

Joined: Sun Feb 24, 2008 12:47 pm
Posts: 4
Try to make a date out of it and a criteria (or hql query and query substitution):

---

Calendar cal = Calendar.getInstance();

cal.set( year, month-1, day );

Date expiryDate = cal.getTime();

---

Criteria criteria = session.createCriteria( Customer.class );

criteria.add( Restrictions.eq( "expiryDate ", expiryDate ) );

return criteria.list();


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 25, 2008 3:55 pm 
Newbie

Joined: Sat Feb 23, 2008 1:18 pm
Posts: 2
thanks larshelge for your help.

As you suggested, I tried date() function of HQL and that worked for me.

Thanks once again!!!

Regards
intecgmbh


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.