-->
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.  [ 8 posts ] 
Author Message
 Post subject: Order on collection size, "Invalid use of group functio
PostPosted: Wed Feb 18, 2004 9:14 am 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:43 am
Posts: 48
I'm trying to select and order by collection size. After two hours of trying to use the examples at [1], I thought i should turn in for help :-)

Consider a class House with a set of residents like :

<class name="House" table="HOUSES">
<!-- ... -->
<set name="residents" table="house_residents"
lazy="true" inverse="true"
cascade="none" order-by="something">
<key column="house"/>
<one-to-many class="Resident"/>
</set>
</class>

I tried stuff like

select house
from House house
left join house.residents resident
group by house
order by count(resident ) desc

but I keep getting "Invalid use of group function"...

any pointers much appreciated :-)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2004 9:32 am 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:43 am
Posts: 48
Has anyone managed to order by the size of a collection usin gMySQL 4+? If yes, can I please have an example?

Thanks,

Manos


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2004 3:49 pm 
Newbie

Joined: Wed Feb 11, 2004 4:48 pm
Posts: 4
you could try something like

session.createSQLQuery("select {house.*} from houses as {house} left join house_residents r on ({house}.id = r.house) group by {house}.id order by count(r.id)","house", House.class);


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2004 5:25 am 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:43 am
Posts: 48
Hi,

Still get a JDBCException following your example:

Caused by: java.sql.SQLException: General error: Invalid use of group function

with

Query qReferers = sess.createSQLQuery(
"select {refHost.*} from HOSTS as {refHost} " +
"left join SESSIONS sess on ({refHost}.id = sess.referingHost ) "+
"group by {refHost}.id order by count(sess.id) LIMIT 30 ",
aliases,
classes );

Any help appreciated,

Manos


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2004 5:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Try to get it right with plain SQL first.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2004 5:54 am 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:43 am
Posts: 48
Unfortunatelly

SELECT HOSTS.id
FROM HOSTS
LEFT JOIN SESSIONS ON ( HOSTS.id = SESSIONS.referingHost )
GROUP BY HOSTS.id
ORDER BY
count( SESSIONS.id )


works just fine in MSSQL but not in MySQL, which is my target DB. I just need a workaround to be able to sort based on collection size in MySQL and was hoping someone has already such a solution and is willing to share it and save my a$$ here ;-)

Thanks,

Manos


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2004 6:01 am 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:43 am
Posts: 48
After googling a little more, I found out MySQL needs an alias for count() in the order by clause to work :

SELECT HOSTS.*, count( SESSIONS.id ) as counted FROM HOSTS LEFT JOIN SESSIONS ON ( HOSTS.id = SESSIONS.referingHost ) GROUP BY HOSTS.id ORDER BY counted;

Which brings us to:

Query qReferers = sess.createSQLQuery("select {refHost}.*, count(sess.id) as counted from HOSTS as {refHost} left join SESSIONS sess on ({refHost}.id = sess.referingHost ) group by {refHost}.id order by counted LIMIT 30 ", aliases,classes );

Can this be mad eto work directly in the MySQL dialect? Using createSQLQuery solves the issue but shoots down portability etc...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 4:23 am 
Beginner
Beginner

Joined: Fri Oct 10, 2003 10:30 am
Posts: 35
Location: Stockholm
Does anybody knows why the alias for the count(*) does not work?
/rob


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