-->
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.  [ 5 posts ] 
Author Message
 Post subject: Use an elapsed time in HQL
PostPosted: Mon Mar 13, 2006 7:23 pm 
Regular
Regular

Joined: Wed Aug 25, 2004 7:40 pm
Posts: 65
The following is a sample code from chapter 14.15 of the reference

Code:
select usr.id, usr.name
from User as usr
    left join usr.messages as msg
group by usr.id, usr.name
order by count(msg)


Now, assume there is a registeration date attribute in the user entity. I want to list the users by the order of the average number of messages per day. I know that I can pass in the current date, but how to use an elapsed time in the HQL? Here is an article on how to cacalculate elapsed time in Java:

http://www.javaworld.com/javaworld/jw-0 ... -time.html

Any thought?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 7:36 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I'd probably workout "day - 1" in java, but if you want to do it in HQL, use the various date functions described in section 14.9, "Expressions", of the ref docs:
Code:
where year(obj.date) = year(:Date)
and month(obj.date) = month(:Date)
and (day(obj.date) = day(:Date) or day(obj.date) = (day(:Date) - 1))
Obivously the logic is wrong, there's no handling of times, first day of month or year, etc., but you get the idea. The HQL logic will be messy, but java Calendars can subtract one day really easily, so IMO you should do the date maths there.


Top
 Profile  
 
 Post subject: A trick that works
PostPosted: Mon Mar 13, 2006 8:05 pm 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
I use a simple trick that works - I map my Date fields to columns of type "long" (we wrote a special user type for this). With this user type in place, I can do the math the same way I do it in Java. An added bonus is that our product works with databases with imprecise date/time types (e.g. MySQL which stores dates with resolutuion too coarse for what we're doing).

Let me know if this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 12:50 am 
Regular
Regular

Joined: Wed Aug 25, 2004 7:40 pm
Posts: 65
Thanks both for the information.

I need to study it since I hadn't see the expression tenwit provided before. And I don't think I will change the current date type data mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 1:02 pm 
Regular
Regular

Joined: Wed Aug 25, 2004 7:40 pm
Posts: 65
After some study, I don't think the tenwit's method won't work since elapsed time calculation is not that simple. Ideally, retrieving the registration date, calculating the elapsed time, and using the result to obtain a user list should be in a single HQL. That doesn't seem to be possible. I am going to create an age entity to keep track of the number of days of each user. A scheduled job will keep the age updated. In this way, I can keep the elapsed time calculation outside of a HQL and obtain the result from entity/table.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.