title: JPA Query Select many-to-one Query with a count
body: <p>So I'm trying to write a JPA query based on the classes below (dumbed down a bit) that will produce the following:</p>
  
  <p>So I have two objects: Thing and Person. A Person can hold a reference to a single Thing. Here are simplified version of the Classes:</p>
  
  <pre><code>public class Thing {
      @Id
      public Long id;
      public String name;
      public String description;
  }
  
  public class Person {
      @Id
      public Long id;
      public String firstname;
      public String lastname;
      @ManyToOne
      public Thing thing;
  }
  </code></pre>
  
  <p>I'm trying to write a JPA query that will give me all the details of every Thing object as well as the number of times that Thing object is referenced by a Person object. Note that a Person could have the value null for Thing. Also a Thing object might not be referenced by any Person object at all but should still be listed.</p>
  
  <p>So given the following tables:</p>
  
  <pre><code>Thing Table
  | id | name | description |
  |  1 | thg1 | a thing     |
  |  2 | thg2 | another one |
  |  3 | thg3 | one more    |
  
  Person Table
  | id | firstname | lastname | thing |
  |  1 | John      | Smith    |     1 |
  |  2 | Simon     | Doe      |     3 |
  |  3 | Anne      | Simmons  |     1 |
  |  4 | Jessie    | Smith    |     1 |
  |  5 | Adam      | Doe      |     3 |
  |  6 | Phil      | Murray   |  null |
  </code></pre>
  
  <p>I would end up with a result like:</p>
  
  <pre><code>| id | name | description | amount |
  |  1 | thg1 | a thing     |      3 |
  |  2 | thg2 | another one |      2 |
  |  3 | thg3 | one more    |      0 |
  </code></pre>
  
  <p>How would I go about writing that JPA Query? (If it makes a difference I'm using the Play Framework 1.2.5)</p>