title: Spring Data JPA return wrong totalElement count after group by
body: <p>I am new to Spring Data JPA and have the something below</p>
  
  <pre><code>        Page&lt;Object&gt; page = tableARepository.findAll(
                  (root, criteriaQuery, criteriaBuilder) -&gt; criteriaQuery
                          .where(sortConfig.getPredicateList(root, criteriaQuery, criteriaBuilder))
                          .groupBy(root.get("colB"))
                          .getRestriction(),
                  sortConfig.getPageable()
          );
  </code></pre>
  
  <p>Everything is correct except for <code>page.getTotalElements()</code> which return the wrong count and I also found out that it is returning the count without <code>groupBy</code>. </p>
  
  <p>I need <code>page.getTotalElements()</code> in order to show the total rows for front end and currently the workaround was I re-query it and get the size of the result which is not very pretty thing to do (the website still in development stage).</p>
  
  <p>I also did print out the hibernate sql query and the select count query did include <code>groupBy</code> but I am not sure what caused it to return the wrong value.</p>
  
  <p>Assume, tableA data as below:</p>
  
  <pre><code>ColA  | ColB | ...
  1       A
  2       B
  3       B
  4       C
  5       D
  </code></pre>
  
  <p>And after <code>groupBy</code>
  I should have result and count below</p>
  
  <pre><code>ColA  | ColB | ...
  1       A
  2       B
  4       C
  5       D
  </code></pre>
  
  <p>totalCount: 4</p>
  
  <p>But somehow I got 5, anyone can help?</p>
  
  <p>NOTE: SortConfig is developed by my colleague to use it for pagination purpose.</p>
  
  <p>EDIT:</p>
  
  <pre><code>select
      count(tblA_.tableA_ID) as col_0_0_ 
  from
      tableA tblA_ 
  where
      tblA_.tableAColC like ? 
  group by
      tblA_.tableAColB
  </code></pre>