code: protected <T> int executeCountQuery(SearchCriteriaBean criteria, EntityManager entityManager, Class<T> type) {
          CriteriaBuilder builder = entityManager.getCriteriaBuilder();
          CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
          Root<T> from = countQuery.from(type);
          countQuery.select(builder.count(from));
          applySearchCriteriaToQuery(criteria, builder, countQuery, from, true);
          TypedQuery<Long> query = entityManager.createQuery(countQuery);
          return query.getSingleResult().intValue();
      }
docs: Gets a count of the number of rows that would be returned by the search.
  @param criteria
  @param entityManager
  @param type