-->
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.  [ 7 posts ] 
Author Message
 Post subject: Problem with join and order by on collection in HQL
PostPosted: Wed Sep 21, 2005 8:09 pm 
Newbie

Joined: Wed Oct 13, 2004 7:32 pm
Posts: 3
Hibernate version:
2.1.8

Code between sessionFactory.openSession() and session.close():
Code:
String query = "select p from Picture p join p.comments c group by p order by count(c) desc";
Query q = s.createQuery(query);
q.setMaxResults(howMany);
q.setCacheRegion("no.uka.intranett.query.gallery");
retList = q.list();


Full stack trace of any exception that occurs:
Quote:
2005-09-22 01:48:45,629 WARN JDBCExceptionReporter:57 - SQL Error: 0, SQLState: 42803
2005-09-22 01:48:45,630 ERROR JDBCExceptionReporter:58 - ERROR: column "picture0_.format" must appear in the GROUP BY clause or be used in an aggregate function
2005-09-22 01:48:45,633 WARN RequestProcessor:509 - Unhandled Exception thrown: class no.uka.intranett.db.DataResourceException
2005-09-22 01:48:45,653 ERROR InsertTag:920 - ServletException in '/secure/gallery/CreateBox.do?command=topCommented': net.sf.hibernate.exception.SQLGrammarException: Could not execute query


And some trace into various struts/tomcat stuff.

Name and version of the database you are using:
PostgreSQL 7.4.7

The generated SQL (show_sql=true):
Quote:
select picture0_.id as id, picture0_.format as format, picture0_.album as album, picture0_.comment as comment, picture0_.uploaded as uploaded, picture0_.uploader as uploader, picture0_.visi
ble as visible, (select avg(hot.rating) from gal_hotornot hot where hot.picture = picture0_.id) as f0_ from gal_pictures picture0_ inner join gal_comments comments1_ on picture0_.id=comments1_.picture group by picture0_.id , com
ments1_.id order by count(comments1_.id)desc limit ?


What have I missed in the HQL query? To me it looks quite similiar to the examples from the reference documentation, but I guess I've missed a detail since it gives bad SQL. Any ideas would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 8:18 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
try hibernate 3 - group by isn't too good in ver < 3.x


Top
 Profile  
 
 Post subject: Same with Hibernate 3.0.5
PostPosted: Thu Sep 22, 2005 2:28 pm 
Newbie

Joined: Wed Oct 13, 2004 7:32 pm
Posts: 3
I just tried it with Hibernate 3.0.5, and the same error occurs. It includes all properties in the SELECT, but not in GROUP BY.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 8:08 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
post bug to JIRA


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 10:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Don't post any bugs in JIRA.

Just include all properties you want to group by in the group by clause.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 3:27 am 
Beginner
Beginner

Joined: Tue Sep 20, 2005 4:32 am
Posts: 29
Location: Cluj-Napoca
Have you tried to execute your generated SQL query directly on your db ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 10:50 am 
Newbie

Joined: Wed Oct 13, 2004 7:32 pm
Posts: 3
gavin wrote:
Just include all properties you want to group by in the group by clause.


Since the generated query issues a select on all the properties of the class, all these properties have to be included in the group by. Is there a simple way to achieve this? Or should I just pick the properties I can't do without and select the explicitly? If so, how do I get the result as a List of objects of the class I'm querying?

It works fine if I add all the other fields in the group by and run it through postgres again.


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