-->
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: HQL issue, GROUP BY generated SQL, Hib 3.0.4
PostPosted: Thu Jun 23, 2005 8:49 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
Hello everyone. I am new to hibernate, but I have a prototype app running with Hib 3.0.4, Oracle dialect so not a super n00b. I have spent lots of time looking into this and searching the archives, but no luck so far. I really appreciate this tool, and help from anyone who might have thoughts on this issue. :)

This post is kind of long with code samples, but the problem is very simple : The GROUP BY generated SQL groups by the ID column when I am retreiving the entire object, so it is trying to select columns that are not included in the GROUP BY clause. I based by syntax exactly like an example in the manual. OK, details:

My problem is with an HQL query using GROUP BY.

Code:
select user
  from User user
  join user.VideoAssets asset
group by user     
order by max(asset.createdDate) desc


the generated sql is:

Code:
select user0_.id as id, user0_.username as username1690_, user0_.password as password1690_
  from MyUser user0_, Video_Asset videoasset1_, Asset videoasset1_1_ 
where user0_.id=videoasset1_.producer_id and videoasset1_.id=videoasset1_1_.id
group by user0_.id
order by max(videoasset1_1_.created_date) desc


And the error (obvious from the SQL) is:

Code:
ORA-00979: not a GROUP BY expression


The generated SQL groups by the ID column, but the select is fetching the whole object.

One of the examples in the doc (http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html#queryhql-grouping ) is very similar:

Code:
select cat
from eg.Cat cat
    join cat.kittens kitten
group by cat
having avg(kitten.weight) > 100
order by count(kitten) asc, sum(kitten.weight) desc


One difference I can see is that my class VideoAsset is a joined-subclass of the Asset object. Is there a different syntax for that? I'll post by hibernate XML below. Thank you to anyone who has taken the time to read this far. I appreciate any thoughts... :)

Code:
<class name="User" table="MyUser">
    <id column="id" name="id" type="long" unsaved-value="0">
        <generator class="native" />
    </id>

    <property column="username" length="1500" name="username" type="string"/>
    <property column="password" length="1500" name="password" type="string"/>
   
    <set name="videoAssets" inverse="true" cascade="save-update">
        <meta attribute="GENERIC">tv.current.base.model.VideoAsset</meta>
        <key column="producer_id" />
        <one-to-many class="VideoAsset" />
    </set>
</class>
<class    name="Asset" table="Asset">
    <id    column="id" name="id" type="long" unsaved-value="0">
        <generator    class="native" />
    </id>

    <many-to-one name="producer" column="producer_id" class="User" lazy="false" />

    <property column="created_date" name="createdDate" type="timestamp"/>

    <joined-subclass    name="VideoAsset" table="Video_Asset">
        <key column="id" />
        <property column="thumbnail" length="1500" name="thumbnail" type="string"/>
        <property column="location" length="1500" name="location" type="string"/>
        <property column="duration" length="1500" name="duration" type="string"/>
        <property column="width" length="1500" name="width" type="string"/>
        <property column="height" length="1500" name="height" type="string"/>
    </joined-subclass>
   
</class>
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 9:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yes, you need to group by all attributes of the entity. There is a long-standing todo to implement a better solution than this.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 3:36 am 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
thank you for the quick reply.

that's a little ugly since my actual class has about 30 attributes, but it is already what I was using as a workaround so I can confirm it works! (I could also use my java code-gen tool to create a public static String within each Java class that contains a coma separated list of all class.attributes which I could then include in my HQL queries with GROUP BY. hmmmm...)

Would updating Hibernate to expand the class into all attributes during SQL generation be a realtivly straightforward fix from a design perspective? If so, and if you point out the classes I should look in to get started, I'll work on a patch...

thanks!
dan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 1:35 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
ps: looking forward to your Hibernate session at JavaOne =)


Top
 Profile  
 
 Post subject: thx
PostPosted: Wed Aug 03, 2005 4:12 pm 
Newbie

Joined: Wed Feb 23, 2005 5:11 pm
Posts: 3
Thanks for posting this info - I ran into the same issue. One additional note: oracle (at least v. 9.x) doesn't allow LOB columns in the GROUP BY clause, so including all columns isn't an option if any are LOBs. I ended up retrieving just the ids ("SELECT new java.lang.Long(mytable.id)...GROUP BY mytable.id"), and then loading the entities in another query. Not ideal, but it works. Any better ideas out there?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 5:20 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
for better results (no only for query like this) you need define lob column in another table (one-to-one) - oracle will save lob in another block and you performance will be better for every query

for example :

tablewithoutlob

id number not null
....

tablewithlob

id number not null -- foreign key tablewithoutlob(id)
blob lobcolumn

regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 8:36 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
gavin wrote:
Yes, you need to group by all attributes of the entity. There is a long-standing todo to implement a better solution than this.


is there an official RFE for this?


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.