Craig Ringer edited a comment on Bug HHH-7611

Argh, JIRA won't let me edit the description. Sorry, I wanted to elaborate.

The test case is one the original reporter (https://hibernate.onjira.com/secure/ViewProfile.jspa?name=windy) greatly simplified down from the real-world problem, as explained in the linked Stack Overflow report.

For testSimplyfiedGroupByObject in the test case (now re-attached here):

public void testSimplyfiedGroupByObject() {
         EntityManager entityManager = buildEntityManager();
         CriteriaBuilder builder = entityManager.getCriteriaBuilder();
         CriteriaQuery<DBObject> query = builder.createQuery(DBObject.class);
         Root<DBObjectAccessCounter> from = query.from(DBObjectAccessCounter.class);
         Path<DBObject> object = from.get(DBObjectAccessCounter_.object);
         query.groupBy(object).select(object);
         entityManager.createQuery(query).getResultList();
}

Hibernate is producing the query:

select dbobject1_.id as id0_ from DBObjectAccessCounter dbobjectac0_ inner join DBObject dbobject1_ on dbobjectac0_.object_id=dbobject1_.id group by dbobjectac0_.object_id;

which fails to execute with:

ERROR:  column "dbobject1_.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: select dbobject1_.id as id0_ from DBObjectAccessCounter dbob...

The correct formulation is:

select dbobject1_.id as id0_ from DBObjectAccessCounter dbobjectac0_ inner join DBObject dbobject1_ on dbobjectac0_.object_id=dbobject1_.id group by dbobject1_.id;

The only change I've made to correct it is that the GROUP BY references the other side of the join condition, the side mentioned in the SELECT list.

This remains the case even if I rephrase the test to explicitly group by the DBObject side:

Path<DBObject> object = from.get(DBObjectAccessCounter_.object);
Path<Long> objectId = object.get(DBObject_.id);
query.groupBy(objectId).select(object);

which seems dead wrong. Looks like a bug to me.

Maybe some RDBMSes infer that the two fields are the same because of the `INNER JOIN` and implicitly transform the first statement into a `GROUP BY` on the `id` of `DBObject`? Or maybe this is just a Pg dialect bug.

This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira