-->
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: GROUP BY Error
PostPosted: Sat Apr 27, 2013 4:06 pm 
Newbie

Joined: Mon Jan 14, 2013 4:07 pm
Posts: 17
Okay, I'm hoping this will be straight forward enough that I don't have to post my model. On the following named query:

Code:
   SELECT c.user
   FROM Contact c
   WHERE c.owner = :ownerID
   GROUP BY c.user.id;

Is giving me an exception stating that GROUP BY is using the userId column from the Contact entity while SELECT is using the column from the entity User.

Additional information, I am only doing the GROUP BY to avoid this query:

Code:
  SELECT DISTINCT c.user FROM Contact c WHERE ...;

Since the distinct would do a sort on ALL fields on the User entity. I suppose if this type of GROUP BY is not supported by Hibernate I could always do the following:

Code:
  SELECT u FROM User u WHERE u.id IN ( SELECT c.user FROM Contact c WHERE ... );

But that would seem less optimal than the GROUP BY.

Thanks!


Top
 Profile  
 
 Post subject: Re: GROUP BY Error
PostPosted: Thu May 02, 2013 4:43 am 
Newbie

Joined: Thu Jul 15, 2010 11:20 am
Posts: 2
This is not an hibernate problem. The sql query supplied to hibernate is not correct.

In Group By clause you can have only those columns which are supplied in select clause or ggregeate functions.

First run you query directly on database and then convert it to Hibernate query.

For example following query will always work..

SELECT c.user.id FROM Contact c WHERE c.owner = :ownerID GROUP BY c.user.id;


Top
 Profile  
 
 Post subject: Re: GROUP BY Error
PostPosted: Thu May 02, 2013 10:50 am 
Newbie

Joined: Mon Jan 14, 2013 4:07 pm
Posts: 17
That's not quite correct, because "user" is being select necessarily means that "user.id" is available for a GROUP BY. This isn't the issue.


Top
 Profile  
 
 Post subject: Re: GROUP BY Error
PostPosted: Thu May 02, 2013 1:34 pm 
Regular
Regular

Joined: Wed Apr 10, 2013 1:02 am
Posts: 50
Your sql is incorrect.

_________________
Regards
Akash Miital
http://akash.thegrassroots.co.in/hibernate/


Top
 Profile  
 
 Post subject: Re: GROUP BY Error
PostPosted: Thu May 02, 2013 1:57 pm 
Newbie

Joined: Mon Jan 14, 2013 4:07 pm
Posts: 17
Can you be more specific Akash? The translated SQL seems to work fine on every database server I've tried it on.


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.