title: JPA create count query from existing CriteriaQuery
body: <p>I have a query with some predicates, I need to count total records for paging.</p>
  <p>Currently, what I'm doing is declare 2 roots for the query to get result list <strong>(1)</strong> and the count query <strong>(2)</strong>, then with each predicate, duplicate it with different root like this</p>
  <pre><code>        CriteriaBuilder cb = em.getCriteriaBuilder();
          CriteriaQuery&lt;A&gt; cq = cb.createQuery(A.class);
          Root&lt;A&gt; root = cq.from(A.class);
          CriteriaQuery&lt;Long&gt; cq = cb.createQuery(A.class);
          Root&lt;A&gt; rootCount = countQuery.from(A.class);
  
          List&lt;Predicate&gt; predicates = new ArrayList&lt;&gt;();
          List&lt;Predicate&gt; predicatesCount = new ArrayList&lt;&gt;();
          
          Predicate p = cb.equal(root.get(A.ID), 1);
          predicates.add(p);
          Predicate p1 = cb.equal(rootCount.get(A.ID), 1);
          predicatesCount.add(p1);
          ...
          // execute both query to get result
  
  </code></pre>
  <p>So the question is:</p>
  <p><strong>Is it possible to create count query from query (1)? Or something to reuse the predicates with count query?</strong></p>
  <p>Thanks for reading!</p>