-->
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.  [ 4 posts ] 
Author Message
 Post subject: Question about HQL and date format?
PostPosted: Mon May 08, 2006 4:35 pm 
Newbie

Joined: Sat Mar 04, 2006 2:50 am
Posts: 3
I want to use raw HQL to query objects based on a data field. So I want to do the equivalent of

"from Event as e where e.data='Jan 20, 2004'"

However, that doesn't work. I assume that my date string isn't properly formatted. I've looked through the forums for HQL example outlining that but couldn't find one. Could someone please give me a simple-minded example that shows the proper date format needed when issuing HQL queries.

Thanks, in advance for any help
Adam


Top
 Profile  
 
 Post subject: data should have read date
PostPosted: Mon May 08, 2006 4:52 pm 
Newbie

Joined: Sat Mar 04, 2006 2:50 am
Posts: 3
I'm interesting in the format where the underlying Java type is java.util.Date. Sorry for confusion.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 5:00 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Why dont you use named parameters, where date format would be taken care of.

Code:
java.util.Date myDateObject = getMyDate( "01/20/2006" );
Query query = session.createQuery("from Event as e where e.data=:myDate" );
query.setParameter( "myDate", myDateObject );


Last edited by bkmr_77 on Mon May 08, 2006 5:02 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon May 08, 2006 5:01 pm 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
If you want to code it into the query then you'll need to use whatever format your database accepts, just like with real SQL, eg with SQL Server:

Code:
from Event as e where e.data='2003-01-06'


or you could use the database date functions.

Or, much better than both of those options, use params - eg:

Code:
Query q = session.createQuery("from Event as e where e.data=?");
q.setDate(dateVal);
q.list();


This means that Hibernate can parameterise the stored procedures so that the same compiled query is used for multiple dates and you don't need to worry about locale or parsing issues.


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