title: using group by with specification in Spring data count method
body: <p>I want to implement a spring data method which check result of the following query</p>
  
  <pre><code>select count(1) from installation_requested_t requested 
  where requested.installation_id in 
        (select installation_id from installation_requested_t where request_id=?) 
  group by requested.installation_id
  having count(1)&gt;1;
  </code></pre>
  
  <p>For the project, it is mandatory to use specification with the spring data jpa.
  So there is my repository method call :</p>
  
  <pre><code>installationRequestedRepository.count(
                                 InstallationSpecs
                                 .existsInstallationRequestLinkedToAnotherEPR(movingEprId)
  );
  </code></pre>
  
  <p>and my spec method:</p>
  
  <pre><code>public static Specification&lt;InstallationRequested&gt; existsInstallationRequestLinkedToAnotherEPR(final Long EPRId) {
      return new Specification&lt;InstallationRequested&gt;() {
  
          @Override
          public Predicate toPredicate(Root&lt;InstallationRequested&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb) {
  
              Subquery&lt;Long&gt; InstallationIdsLinkedToEPRsubquery = query.subquery(Long.class);
  
              InstallationIdsLinkedToEPRsubquery.select(root.get(InstallationRequested_.id))
                      .where(cb.equal(root.get(InstallationRequested_.environmentPermitRequestId), EPRId));
  
              Subquery&lt;InstallationRequested&gt; installationRequestForTheseInstallationIdsWhoAreLinkedToAnotherEPR =
                      query.subquery(InstallationRequested.class);
  
              installationRequestForTheseInstallationIdsWhoAreLinkedToAnotherEPR
                      .select(installationRequestForTheseInstallationIdsWhoAreLinkedToAnotherEPR
                              .from(InstallationRequested.class))
                      .where(root.get(InstallationRequested_.id).in(InstallationIdsLinkedToEPRsubquery))
                      .groupBy(root.get(InstallationRequested_.id))
                      .having(cb.greaterThan(cb.count(cb.literal(1)), cb.literal(1l)));
  
              return cb.exists(installationRequestForTheseInstallationIdsWhoAreLinkedToAnotherEPR);
  
          }
      };
  
  }
  </code></pre>
  
  <p>And my problem is that everything doesn't execute well:</p>
  
  <pre><code>org.springframework.dao.InvalidDataAccessApiUsageException: 
  org.hibernate.hql.internal.ast.QuerySyntaxException: 
  unexpected token: where near line 1, column 333 
  [select count(generatedAlias0) from be.question.domain.model.request.installation.InstallationRequested as generatedAlias0 
  where exists 
      (select generatedAlias1 from be.question.domain.model.request.installation.InstallationRequested as generatedAlias1 
      where generatedAlias0.id in 
          (select generatedAlias0.id from  where generatedAlias0.environmentPermitRequestId=1072487L) 
      group by generatedAlias0.id having count(1)&gt;1L)
  ]; 
  </code></pre>
  
  <p>Do you have an idea to make my spec work and another way to make the asked task ?</p>