Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JPA fetchCount() and group by not working #1614

Closed
intuitiveminds opened this issue Oct 19, 2015 · 8 comments · Fixed by #2605
Closed

JPA fetchCount() and group by not working #1614

intuitiveminds opened this issue Oct 19, 2015 · 8 comments · Fixed by #2605

Comments

@intuitiveminds
Copy link

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!

@intuitiveminds
Copy link
Author

Any updates on this issue?

@mosekk
Copy link

mosekk commented May 7, 2018

Have the same, didn't fixed it in 4.1.4

@ghost
Copy link

ghost commented May 18, 2018

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.

@mnoky
Copy link

mnoky commented Aug 17, 2018

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, ...)?

@bluebilt
Copy link

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

@engineer-myoa
Copy link

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.

@gentaliti
Copy link

gentaliti commented Jul 28, 2020

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

@jwgmeligmeyling
Copy link
Member

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

@querydsl querydsl locked as resolved and limited conversation to collaborators Dec 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
8 participants