Repository: querydsl/querydsl

Issue #2053: Fetching `QueryResults` from a query having both `groupBy` and `having` clause results in a `QuerySyntaxException`
Created: 2016-11-18T14:05:47Z
Fetching QueryResults from a query having both groupBy and having clause results in a QuerySyntaxException. Here are two test cases, the first one executes without error the second results in the above described error:
    @Test
    public void groupByHavingFetch() {
        QCustomer customer = QCustomer.customer;
        JPAQuery<Customer> customerQuery = new JPAQuery<Customer>(entityManager, HQLTemplates.DEFAULT).from(customer);

        customerQuery.groupBy(customer.lastName).having(customer.id.count().gt(1)).fetch();
    }

    @Test
    public void groupByHavingFetchResults() {
        QCustomer customer = QCustomer.customer;
        JPAQuery<Customer> customerQuery = new JPAQuery<Customer>(entityManager, HQLTemplates.DEFAULT).from(customer);

        customerQuery.groupBy(customer.lastName).having(customer.id.count().gt(1)).fetchResults();
    }

The test in error generates following error:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: having near line 3, column 1 [select count(distinct customer.lastName)
from Customer customer
having count(customer.id) > ?1]

	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:131)
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
	at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662)
	at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)
	at com.querydsl.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:101)
	at com.querydsl.jpa.impl.AbstractJPAQuery.fetchResults(AbstractJPAQuery.java:211)
	at GroupByHavingTest.groupByHavingFetchResults(GroupByHavingTest.java:49)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: having near line 3, column 1 [select count(distinct customer.lastName)
from Customer customer
having count(customer.id) > ?1]
	at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74)
	at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:91)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:288)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:187)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
	at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
	at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
	at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
	at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:545)
	at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:654)
	... 32 more

The resulting hql Statement is missing the groupBy clause.

Duplicate issue #2378: fetchResults() skip 'group by'
Created: 2018-11-13T09:53:43Z
the generated  count query skip the 'group by' in this case

there may be something wrong with this judge:

Closing comment (2020-05-28T14:38:55Z): Its not a bug, the group by clause in the case of forCountRow is handled in the code fragment above: 
  
    
      querydsl/querydsl-jpa/src/main/java/com/querydsl/jpa/JPQLSerializer.java
    
    
        Lines 178 to 180
      in
      69e7478
    
  
  
    

        
          
           if (!groupBy.isEmpty()) { 
        

        
          
               append(SELECT_COUNT_DISTINCT); 
        

        
          
               handle(", ", groupBy); 
        
    
  


There are limitations of using fetchResults() and fetchCount() for queries with a having clause however. This is a duplicate of #2053.