-->
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: how to do aggregate HQL query properly
PostPosted: Tue Aug 15, 2006 8:37 am 
Newbie

Joined: Fri Jan 27, 2006 11:14 am
Posts: 12
Hi, i wouldlike to use aggregate functions in HQL, and i got such problems while experimenting with the query...

i have a class Booking like

Booking(id, tutor,week,package,date,fee,term,note,student,order)

i wouldl like to do a query as such : "

"SELECT DISTINCT tutor, week, COUNT(DISTINCT student) AS numOfStudents
FROM Booking WHERE term=:tm GROUP BY
tutor,week"

problem 1 is - the query does not work because i got an error telling me that group by clause must include all variables of this Booking class, i e , i changed my query to

"SELECT DISTINCT tutor, week, COUNT(DISTINCT student) AS numOfStudents
FROM Booking WHERE term=:tm GROUP BY
tutor,week, id, package, date,fee,term,note,student,order"

and it works. however, it seems that the query produces a list of Booking objects, not only the variables tutor, week, and numOfStudents as specified in the SELECT clause. as a result i got 2nd problem

problem 2 - i actually defined another class called "Rota(tutor, week, numOfStudents)"

and i did
Rota t = (Rota) queryResult.get(0);

and i got a ClassCastException. i guess the reason is that the query produces a list of Booking objects other than the only selected variables as in the select clause; therefore i changed to
Booking b =(Booking) queryResult.get(0);

unsurprisingly it doesnt work i guess its because in the select clause i have another variable "numOfStudnets" which is not defined in Booking.

i dont know what to do... i certainly donot want to add a variable of "numOfStudent" to Booking class.. but i dont know any other solutions to this problems. seems HQL makes aggregate query much more complicated, or im doin this in totally wrong way?
all in all, all i want to do is to get the values of "tutor, week, and numberOfStudents" from the query, nothing else.

any ideas please help! thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 9:09 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Maybe you can try this approach regarding the rota class that you have:

"SELECT DISTINCT new Rota(tutor, week, COUNT(DISTINCT student))
FROM Booking WHERE term=:tm GROUP BY
tutor,week, id, package, date,fee,term,note,student,order"


I haven't tried this in a test case but I guess it could work out for you. This way you should avoid the ClassCastException.

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 9:20 am 
Newbie

Joined: Fri Jan 27, 2006 11:14 am
Posts: 12
thanks so much! its working now!

hmm i still dont understand why the generated hql selects all variables of Booking other than the only specified in the query... isnt it more expensive in terms of the cost of query?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 15, 2006 9:35 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Usually colum reads are not so expensive (unless you are reading a LOB).

_________________
Please don't forget to rate


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.