Querydsl: JPA fetchCount() and group by not working

Created on 19 Oct 2015  路  8Comments  路  Source: querydsl/querydsl

This is a bug resulting from this posting on Google Groups (see https://groups.google.com/forum/#!topic/querydsl/WH582YIY98I):

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);
    }

My wish would be that a count query with group by + having would work, because I have a use case where I need exactly that. If that doesn't work out, then at least the having part should be omitted when fetchCount() is called.

Thanks in advance (for fixing this bug) and thanks for QueryDSL in general, which is truly awesome!

jpa progress

Most helpful comment

Have the same, didn't fixed it in 4.1.4

All 8 comments

Any updates on this issue?

Have the same, didn't fixed it in 4.1.4

I can also confirm this is present at least up to 3.7.4

.groupBy().count() is trying to shove null into a long primitive.

workaround is using .groupBy(element).singleResult(element.count()) that returns Long object and coalesce in code until the bug is fixed.

I'm also hitting this problem, using version 4.2.1. My query has multiple expressions in the groupBy() clause and singleResult() is no longer available in QueryDSL 4. Any ideas on how to apply the workaround above when doing .groupBy(element1, element2, ...)?

Also still running in to this unfortunately. Has anyone found a workaround?
(Other then having to use native JPA)

Still raised same error in 4.2.1
@svenpanko point out exactly, so I removed all having expressions..
Even need it, change native sql temporarily.

Any progress in this? It's been 5 years...

Yes, we're going to deprecate/remove it entirely for JPA: https://github.com/querydsl/querydsl/pull/2605 . The computation will fall back to an implementation in memory, as it simply can not be done with plain JPA.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

palexis picture palexis  路  12Comments

LilithBristol picture LilithBristol  路  10Comments

rocketraman picture rocketraman  路  18Comments

glau2 picture glau2  路  15Comments

amanteaux picture amanteaux  路  11Comments