title: Java Hibernate Count Rows
body: <p>I am using Java8 with Hibernate5 and JPA2.  I would like to count the number of rows in a result set. I have the following code that works, however, I would like to know if there's a more efficient way of doing it? I think the code below first queries the entire result set and the counts the rows. </p>
  
  <pre><code>    final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
      final CriteriaBuilder criteriaBuilder = entityManagerFactory.getCriteriaBuilder();
      CriteriaQuery&lt;Rating&gt; criteria = criteriaBuilder.createQuery(Rating.class);
      Root&lt;Rating&gt; root = criteria.from(Rating.class);
      ParameterExpression&lt;Job&gt; param = criteriaBuilder.parameter(Job.class);
  
      TypedQuery&lt;Rating&gt; queryRating = entityManager.createQuery(criteria);
      queryRating.setParameter(param, job);
      int results = queryRating.getResultList().size();
  </code></pre>
  
  <p>Is there a way of rather making the SQL do a <code>count(*)</code>?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Thanks to @chsdk below, I have a revised version of code:</p>
  
  <pre><code>    CriteriaBuilder qb = entityManager.getCriteriaBuilder();
      CriteriaQuery&lt;Long&gt; cq = qb.createQuery(Long.class);
      cq.select(qb.count(cq.from(Rating.class)));
      cq.where(/*your stuff*/);
      return entityManager.createQuery(cq).getSingleResult();
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>How do I set the <code>where</code> clause with the <code>Job</code> parameter?</p>
  
  <p><strong>More info:</strong></p>
  
  <pre><code>+--------+   +------------+    +-----+
  | rating |   | rating_job |    | job |
  +--------+   +------------+    +-----+
  |   ID   |   |   RAT_ID   |    |  ID |
  |        |   |   JOB_ID   |    |     |
  +--------+   +------------+    +-----+
  </code></pre>
  
  <p><strong>Rating.java</strong></p>
  
  <pre><code>@ManyToOne(fetch=FetchType.EAGER)
  @JoinTable
  (
      name="rating_job",
      joinColumns={ @JoinColumn(name="RAT_ID", referencedColumnName="ID") },
      inverseJoinColumns={ @JoinColumn(name="JOB_ID", referencedColumnName="ID") }
  )
  private Job job;
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Thanks to @chsdk, here is my version that works:</p>
  
  <pre><code>    final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
      final CriteriaBuilder criteriaBuilder = entityManagerFactory.getCriteriaBuilder();
      CriteriaQuery&lt;Long&gt; criteria = criteriaBuilder.createQuery(Long.class);
      Root&lt;Rating&gt; root = criteria.from(Rating.class);
      ParameterExpression&lt;Job&gt; param = criteriaBuilder.parameter(Job.class);
      criteria.select(criteriaBuilder.count(root)).where(criteriaBuilder.equal(root.get("job"), param));
      TypedQuery&lt;Long&gt; queryRating = entityManager.createQuery(criteria);
      queryRating.setParameter(param, job);
      Long results = queryRating.getSingleResult();
  
      return results;
  </code></pre>