-->
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: Comparing Dates in HQL
PostPosted: Thu May 11, 2006 10:54 am 
Newbie

Joined: Wed Feb 09, 2005 1:54 pm
Posts: 3
Hibernate version:3.1

I want to compare dates in HQL, my model uses java.util.Date to map a oracle DATE column. Basically I need:

-- Oracle SQL would be like this
select * from a where trunc(a.dateAndTime) between trunc(?) and trunc(?)

Hibernate does not allow me the use of "trunc()" because it maps the parameters as TIMESTAMP (when setting the preparedstatement parameter value) - and node that the database if of type DATE.

Please note that the "trunc()" function is needed because oracle's DATE field contains date and time, and that "trunc()" does not operate on TIMESTAMP's.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 12:50 pm 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
Are you using query.setTimestamp() to set your params? If so, try using query.setDate() and then a query like:-

Code:
select * from a where trunc(a.dateAndTime) between ? and ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 1:04 pm 
Newbie

Joined: Wed Feb 09, 2005 1:54 pm
Posts: 3
I am not, Hibernate is doing it in the prepared statement it creates.

My current HQL is this one:

select new com.bcsinfo.tsheet.model.TSCalendarValue(ts.Date,sum(ts.UT))
from TimeSheet as ts
where ts.Date between :dFrom and :dTo
group by ts.Date
order by ts.Date

This gives me a Calendar of the sums of ts.UT, and I have many other uses of date and between. This HQL should be (if you think of oracle and it's DATE type):

select new com.bcsinfo.tsheet.model.TSCalendarValue(ts.Date,sum(ts.UT))
from TimeSheet as ts
where trunc(ts.Date) between trunc(:dFrom) and trunc(:dTo)
group by ts.Date
order by ts.Date

But this does not work because of trunc does not operate on a timestamp in oracle. Ant it also seams wrong to me (not portable). I would like to do:

--WRONG
select new com.bcsinfo.tsheet.model.TSCalendarValue(ts.Date,sum(ts.UT))
from TimeSheet as ts
where date(ts.Date) between date(:dFrom) and date(:dTo)
group by ts.Date
order by ts.Date
-- End of WRONG

But "date()" does not exist, I could only find: day(), month(), year(). But since I am using parameters I am not even trying to use them.


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.