title: Total row count for pagination using JPA Criteria API
body: <p>I am implementing "Advanced Search" kind of functionality for an Entity in my system such that user can search that entity using multiple conditions(eq,ne,gt,lt,like etc) on attributes of this entity. I am using JPA's Criteria API to dynamically generate the Criteria query and then using <code>setFirstResult()</code> &amp; <code>setMaxResults()</code> to support pagination. All was fine till this point but now I want to show total number of results on results grid but I did not see a straight forward way to get total count of Criteria query.<br>
  This is how my code looks like:</p>
  
  <pre><code>CriteriaBuilder builder = em.getCriteriaBuilder();
  CriteriaQuery&lt;Brand&gt; cQuery = builder.createQuery(Brand.class);
  Root&lt;Brand&gt; from = cQuery.from(Brand.class);
  CriteriaQuery&lt;Brand&gt; select = cQuery.select(from);
  .
  .
  //Created many predicates and added to **Predicate[] pArray**
  .
  .
  select.where(pArray);
  // Added orderBy clause
  TypedQuery typedQuery = em.createQuery(select);
  typedQuery.setFirstResult(startIndex);
  typedQuery.setMaxResults(pageSize);
  List resultList = typedQuery.getResultList();
  </code></pre>
  
  <p>My result set could be big so I don't want to load my entities for count query, so tell me efficient way to get total count like <code>rowCount()</code> method on Criteria (I think its there in Hibernate's Criteria).</p>