title: JPA and aggregate functions. How do I use the result of the query?
body: <p>I'm new to ORM stuff and I need some help understanding something.</p>
  
  <p>Let's assume I have the following standard SQL query:</p>
  
  <pre><code>SELECT *, COUNT(test.testId) AS noTests FROM inspection
  LEFT JOIN test ON inspection.inspId = test.inspId
  GROUP BY inspection.inspId
  </code></pre>
  
  <p>which I want to use in JPA.</p>
  
  <p>I have an Inspection entity with a one-to-many relationship to a Test entity. (an inspection has many tests)
  I tried writing this in JPQL:</p>
  
  <pre><code>Query query = em.createQuery("SELECT insp, COUNT(???what???) " +
        "FROM Inspection insp LEFT JOIN insp.testList " +  
        "GROUP BY insp.inspId");
  </code></pre>
  
  <p>1) How do I write the COUNT clause? I'd have to apply count to elements from the test table but testList is a collection, so I can't do smth like <code>COUNT(insp.testList.testId)</code></p>
  
  <p>2) Assuming 1 is resolved, what type of object will be returned. It will definitely not be an Inspection object... How do I use the result? </p>