-->
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.  [ 1 post ] 
Author Message
 Post subject: [JPA] Problem with query
PostPosted: Wed Nov 26, 2008 11:16 am 
Newbie

Joined: Sat Oct 28, 2006 3:05 pm
Posts: 10
Hibernate version:
Entity Manager: 3.4.0.GA, the rest are bundled libs

Hi. I am no sure if this is the right place to ask this, but...
I am currently learning EJB3 and JPA, I am using Glassfish v2, and I use the default TopLink essentials and Hibernate as providers to test this and that.

There is a class Item: id, name, desc;
and Bid: id, value, item_id

so the relationship is one-to-many from Item to Bid (so that one Item has multiple Bids).

The query I am using is:

select b.item, avg(b.value) from Bid b group by b.item

and it returns an exception, and the root cause is: "not a group by expression". When I look at the generated SQL, it turns out that it groups by the bid.item_id field, and the select wants all fields from Item, and so it really is incorrect:

select bid0_.ITEM_ID as col_0_0_, avg(bid0_.VALUE) as col_1_0_, item1_.ID as ID2_, item1_.DESCRIPTION as DESCRIPT2_2_, item1_.NAME as NAME2_
from BID bid0_ inner join ITEM item1_ on bid0_.ITEM_ID=item1_.ID group by bid0_.ITEM_ID

so obviously the generated sql is not right. The thing is, that TopLink does cope with this, because the ORDER BY clause has all the fields that item has, and so the sql query is correct.

The question is: is this bahaviour not specified in the specs, and so different providers can behave differently, or am I simply missing on something (some property I need to set)? I am using a book to learn EJB3 and JPA, and it has an analogical relationship (one user, many categories) and query (SELECT c.user, COUNT(c.categoryId) FROM Category c GROUP BY c.user), and they say it works fine.

Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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