title: Need help creating JPA criteria query
body: <p>I'm building my first Java EE web application using Glassfish and JSF. I'm fairly new to the criteria query and I have a query I need to perform but the javaee6 tutorial seems a little thin on examples. Anyway, I'm having a hard time creating the query.</p>
  
  <p>Goal: I want to pull the company with the most documents stored. 
  Companies have a OneToMany relationship with Documents. 
  Documents has a ManyToOne relationship with several tables, the "usertype" column distinguishes them.</p>
  
  <p>MySQL query: </p>
  
  <pre><code>SELECT USERID, COUNT(USERID) AS CNT 
  FROM DOCUMENTS 
  WHERE USERTYPE="COMPANY" 
  GROUP BY USERID 
  ORDER BY CNT DESC
  </code></pre>
  
  <p>Thanks</p>
  
  <p>--update--
  Based on user feedback, here is what I have so far:</p>
  
  <pre><code>        CriteriaBuilder cb = em.getCriteriaBuilder();
          CriteriaQuery&lt;Documents&gt; cqry = cb.createQuery(Documents.class);
          //Intersting Stuff
          Root&lt;Documents&gt; root = cqry.from(Documents.class);
          Expression userid = root.get("userID");
          Expression usertype = root.get("userType");
          Expression count = cb.count(userid);
          cqry.multiselect(userid, count);
          Predicate userType = cb.equal(usertype, "COMPANY");
          cqry.where(userType);
          cqry.groupBy(userid);
          cqry.orderBy(cb.desc(count));
          //more boilerplate
          Query qry = em.createQuery(cqry);
          List&lt;Documents&gt; results = qry.getResultList();
  </code></pre>
  
  <p>The error I get is:</p>
  
  <pre><code>Exception Description: Partial object queries are not allowed to maintain the cache or be edited.  You must use dontMaintainCache().
  </code></pre>
  
  <p>Typical error, means nothing to me!</p>