title: JPA Criteria query sum with Spring Data Pageable
body: <p>
  I have a method in repository:</p>
  
  <pre class="lang-java prettyprint-override"><code>public Long sumOfPrices(Specification&lt;Order&gt; spec) {
      CriteriaBuilder builder = em.getCriteriaBuilder();
      CriteriaQuery&lt;Long&gt; query = builder.createQuery(Long.class);
      Root&lt;Order&gt; root = query.from(Order.class);
      query.select(builder.sum(root.get(Order_.price)));
      query.where(spec.toPredicate(root, query, builder));
      return sum = em.createQuery(query).getSingleResult();
  }
  </code></pre>
  
  <p>How to write a method with pageable?</p>
  
  <pre class="lang-java prettyprint-override"><code>public Long sumOfPrices(Specification&lt;Order&gt; spec, Pageable pageable)
  </code></pre>
  
  <p>I don't know where to call setMaxResult and setFirstResult, because sum returns a single result.</p>