QueryDSL 4.x, JPA, fetchCount() and group by clause

744 views
Skip to first unread message

Sven Panko

unread,
Oct 15, 2015, 3:53:35 PM10/15/15
to Querydsl
Hey all,

I found a strange problem with a rather complex group by query when it comes to counting the number of rows that will be returned for the query - the created HQL query contains a having clause (which is correct), but omits the group by clause (which is not correct and leads to an exception, stating that having was encountered without group by). I debugged the problem and found this code in JPQLSerializer.serialize():

        // group by
       
if (!groupBy.isEmpty() && !forCountRow) {
            append
(GROUP_BY).handle(COMMA, groupBy);
       
}

       
// having
       
if (having != null) {
            append
(HAVING).handle(having);
       
}


If there is any reason why group by in count queries should not work then this code is wrong, because if the group by is omitted, clearly the having clause must also be omitted. On the other hand, if omitting the groupBy clause when forCountRow is true is wrong, then this is also a bug. Can someone please clarify why group by is omitted in count cases?

Thanks in advance

Sven

timowest

unread,
Oct 16, 2015, 11:33:30 AM10/16/15
to Querydsl
Yes, this is a bug, we didn't take having into account when doing the groupBy + count handling. Could you open an issue for this?

Sven Panko

unread,
Oct 19, 2015, 8:23:56 AM10/19/15
to Querydsl
Hi Timo,

sure, I opened issue #1614 just now. A short question here - will the fix enable fetchCount() with group by or will you omit the having clause? If the latter is the case - is there any way for me to do a count of rows for a group by query with QueryDSL + JPA?

Thanks again

Sven

timowest

unread,
Oct 19, 2015, 3:07:03 PM10/19/15
to Querydsl
There isn't yet a fix available, but you will see in the coming days how it looks like and which restriction possibly apply.

krok...@gmail.com

unread,
May 8, 2018, 3:54:47 AM5/8/18
to Querydsl
Still not fixed 
Reply all
Reply to author
Forward
0 new messages