-->
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.  [ 6 posts ] 
Author Message
 Post subject: newbie stuck on Date manipulation in HQL
PostPosted: Tue Mar 02, 2004 7:55 pm 
Newbie

Joined: Mon Jan 05, 2004 11:27 am
Posts: 4
Location: US
Hello,
I am trying to do the following to compare a property with today's date.
The property is of type java.sql.Timestamp.
All of the following failed:

Code:
from myObject as o  where o.Timestamp  < new java.util.Date()

from myObject as o 
where o.Timestamp  < new java.sql.Timestamp( System.currentTimeMillis() )


the manual is very quiet on date manipulation.
Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 11:46 pm 
Beginner
Beginner

Joined: Tue Jan 27, 2004 2:14 pm
Posts: 40
Location: Atlanta, GA, USA
Try something like this....

Code:
Date d = new Date();         
Timestamp ts = new Timestamp(d.getTime());

String hqlString = "select obj from Object as obj where obj.TimestampField < :timeStampField";

Query query = session.createQuery(hqlString);
query.setTimestamp("timeStampField",ts);
             
List list = query.list();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 1:13 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Be very careful when comparing dates and timestamps. Some pointers:

1) Although a Timestamp is Date, the equals method may fail "unexpectedly" due to the time component of a timestamp. Check out the javadocs of the the two classes. I always avoid comparing dates and timestamps directly using equals, and use a custom class to do the grunt work.

2) When comparing dates using an expression in a HQL query, you are really making use of the database vendors implementation of data comparison. If you are experiencing problems, log the bind parameters to the SQL query and you should be able to work it out from there.

Justin


Top
 Profile  
 
 Post subject: hmmm
PostPosted: Wed Mar 03, 2004 9:34 am 
Newbie

Joined: Mon Jan 05, 2004 11:27 am
Posts: 4
Location: US
This seems pretty cool. The problem is that I am working under another developer
that knows hibernate a lot more than I do. He has provided an API in which I can create
my own hql queries and use them in my programs. I no means (I think) to do any session.createQuery() stuff. MY basic problem is this. I need to figure out how to
find persisted objects that are older than hour (+ other criteria) so I can delete them.
Using the api I have I can do deleteObject("from myObject o where id =?) with no
problem. My problem is how to select on a timestamp field minus 1 hour and represent that in an hql query. Thanks Guys!

tcollins wrote:
Try something like this....

Code:
Date d = new Date();         

Timestamp ts = new Timestamp(d.getTime());

String hqlString = "select obj from Object as obj where obj.TimestampField < :timeStampField";

Query query = session.createQuery(hqlString);
query.setTimestamp("timeStampField",ts);
             
List list = query.list();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 9:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well just create a timestamp "current date minus one hour" in your java code and set it as parameter for an HQL query like "from X x where x.time < ?"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 8:58 pm 
Newbie

Joined: Mon Jan 05, 2004 11:27 am
Posts: 4
Location: US
You are right, I was making it a lot more complicated than it had to be.


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