简体   繁体   中英

Grails: “Not in aggregate function” problem with HQL query

Querying by HQL with

select a from Article a join a.tags t 
where t.name in (:tags) 
group by a 
having count(t)=:tag_count

using HSQLDB in Grails produces an SqlException "Not in aggregate function...". Looking at the generated SQL provides

SELECT a.id, a.title, a.url, ....
..
GROUP BY a.id
HAVING ..

In some MySQL products this works, I have heard, but apparently not in HSQLDB. I don't understand:

  1. Why does Hibernate generate faulty SQL? Or does it not?
  2. Why is HSQLDB not allowing the grouping on just the id while selecting the lot, I mean its the primary key after all and my SELECT does not introduce indeterministic values such as a random or current time

Just found this bug in Hibernate issue tracker. It seems this is a known problem that has been around for five years.... Unbelievable. Its always amazing to see that seemingly fundamental things can be broken for such a long time.

The generated SQL is not very right, since we should GROUPBY all non-aggregated properties in SELECT clause. For example:

SELECT name,category FROM books
GROUP BY category;

The above query will give false data, because it will only give 1 rows for a category(even if running in MySQL). Instead, it should be done like this:

SELECT name,category FROM books
GROUP BY name,category;

EDIT : After some more investigating, I realize that MySQL is the one who doesn't follow the rule. For some sake of query optimization, MySQL does permit not full GROUP BY query(by default, there's an option to turn this function off). If you run the query on other databases like Oracle or HSQLDB, you will get errors. The detail answer is found in this question .

EDIT2 : To re-write the query by your way, there's no other mean except listing all non-aggregation selected properties in Group By clause. From Hibernate doc :

Hibernate also does not currently expand a grouped entity, so you cannot write group by cat if all properties of cat are non-aggregated. You have to list all non-aggregated properties explicitly.

@Tom: But I think that maybe you don't need all the fields in Article table. If that is the case, would you mind provide more information about the domain class and what you are going to query, so we can see the problem clearer?

This type of query is actually allowed by the current SQL Standard so long as ID is a primary key column. But this is considered an optional feature of the Standard and not required to be supported by database engines.

HSQLDB 2.x supports this when ID is a primary key column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM