title: Solving JPA query finding the last entry in connected list
body: <p>Following class structure is given:</p>
  
  <pre><code>class Job 
  {
      String description;
      Collection&lt;JobHistory&gt; history;
  }
  
  class JobHistory
  {
      Date assignDate;
      User jobOwner;
  }
  
  class JobOwner 
  {
      String name;
      String id;
  }
  </code></pre>
  
  <p>This class-structure is accessible on the db via JPA. In the DAO-Layer I can write queries in JPA syntax.</p>
  
  <p>The Problem: I want a list with <code>Job</code> and <code>JobHistory</code> entries for a given owner with given id and who is the last one in the <code>Jobhistory</code> of the job (ordered by assignDate). Sounds quite complicated, perhaps simpler: give me all jobs and <code>JobHistory</code> where specified owner is the actual owner of the job.</p>
  
  <p><strong>Update:</strong> for clarity I will slightly change the names of the classes.</p>
  
  <pre><code>class Job 
  {
      String description;
      Collection&lt;JobOwnerHistory&gt; history;
  }
  
  class JobOwnerHistory
  {
      Date assignDate;
      User jobOwner;
  }
  
  class JobOwner 
  {
      String name;
      String id;
  }
  </code></pre>
  
  <p>Every <code>Job</code> has a history of his owners sorted by <code>assignDate</code>. The actual owner got the job last assigned (i.e. <code>MAX(assignDate)</code>). I want find for every job the <code>JobOwnerHistory</code> entry with <code>MAX(assignDate)</code> for a specific user <code>User</code>.</p>