title: Spring-Data-JPA Specification with Aggregate function
body: <p>How can we write below query using Specification</p>
  
  <pre><code>SELECT e.id, e.name, count(e.id) 
  FROM Employee e INNER JOIN Department d
  WHERE e.id = d.empId;
  </code></pre>
  
  <p>When we are using Specification, it is not selecting count() even it is mentioned in multiselect.</p>
  
  <p>Any idea, how to include count column in Specification when we are using JPA <strong>findAll</strong> method.</p>
  
  <p>Tried with multiselect, but when it passed to specification then it query only on non-aggregated columns.</p>
  
  <pre><code>Expression&lt;Long&gt; countExp = cb.count(root.get("id"));
   CriteriaQuery&lt;Employee&gt; select =
   criteriaQuery.multiselect(root.get("id"), root.get("name"), countExp);
  </code></pre>
  
  <p>But it generates query like :</p>
  
  <pre><code>SELECT e.id, e.name
  FROM Employee e INNER JOIN Department d
  WHERE e.id = d.empId;
  </code></pre>
  
  <p>Seems having same issue as: 
  <a href="https://stackoverflow.com/questions/14204841/jpaspecificationexecutor-complex-queries-with-specifications">JpaSpecificationExecutor : complex queries with specifications</a></p>
  
  <p><a href="https://stackoverflow.com/questions/28852670/why-the-multiselect-method-in-jpa-does-not-work">Why the multiselect method in JPA does not work</a></p>