title: How do I retrieve the number of distinct rows with the JPA Criteria API
body: <p>I need to count the number of rows returned by a <code>CriteriaQuery</code>. The relevant piece of information here is that those need to be distinct rows based on a dynamically selected set of <strong>columns</strong>.</p>
  <p>This means, that I cannot count over the entire table, since that might consider results, that would be redundant if you strip a certain number of columns.</p>
  <p>I have a List of Predicates and Selections to conform to:</p>
  <pre><code> private final List&lt;Selection&lt;?&gt;&gt; projection = new ArrayList&lt;&gt;();
   private final List&lt;Predicate&gt; predicates = new ArrayList&lt;&gt;();
  </code></pre>
  <p>and I want to count the number of rows that would be returned, if this query was executed non-paginated:</p>
  <pre><code>criteriaQuery.multiselect(projection)
          .distinct(true)
          .where(cb.and(predicates.toArray(new Predicate[0])));
  </code></pre>
  <p>The usual approach of transforming this into a <code>Subquery</code> will not work, since you cant <code>multiselect</code> on a <code>Subquery</code> and also can't select from the <code>Subquery</code>.</p>
  <p>Can this be done with the Criteria API?</p>