title: Subquery in select clause with JPA Criteria API
body: <p>I'm trying, as in title, to insert a subquery in select clause like in this simple SQL:</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT id, name, (select count(*) from item) from item
  </code></pre>
  
  <p>this is obviously only a mock query just to make my point. (The point would be to get the last invoice for each item returned by the query.)</p>
  
  <p>I've tried this:</p>
  
  <pre class="lang-java prettyprint-override"><code>CriteriaBuilder cb = em.getCriteriaBuilder();
  CriteriaQuery&lt;Tuple&gt; c = cb.createTupleQuery();
  Root&lt;Item&gt; item= c.from(Item.class);
  
  Subquery&lt;Long&gt; scount = c.subquery(Long.class);
  Root&lt;Item&gt; sarticolo = scount.from(Item.class);
  scount.select(cb.count(sitem));
  
  c.multiselect(item.get("id"),item.get("nome"), scount);
  
  Query q = em.createQuery(c);
  q.setMaxResults(100);
  List&lt;Tuple&gt; result = q.getResultList();
  
  for(Tuple t: result){
    System.out.println(t.get(0) + ", " + t.get(1) + ", " + t.get(2));
  }
  </code></pre>
  
  <p>but I only get:</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException:
    Subquery cannot occur in select clause</p>
  </blockquote>
  
  <p>How can I get a similar result? </p>