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.
|