title: EJB3/JPA entity with an aggregated attribute
body: <p>I wanted to know if there is a way to get in a One2Many relationship a field of the One side that is an aggregate of the Many side.</p>
  
  <p>Let's take the following example:</p>
  
  <pre><code>@Entity
  public class A {
   @Id
   private Long id;
   @OneToMany (mappedBy="parentA")
   private Collection&lt;B&gt; allBs;
  
   // Here I don't know how to Map the latest B by date
   private B latestB;
      // Acceptable would be to have : private Date latestBDate;
  }
  
  @Entity
  public class B {
   @Id
   private Long id;
   private Date date;
   @ManyToOne (targetEntity=A.class)
   private A parentA;
  }
  </code></pre>
  
  <p>My question is how can I make the mapping of the field latestB in the A entity object without doing any de-normalization (not keeping in sync the field with triggers/listeners)? </p>
  
  <p>Perhaps this <a href="https://stackoverflow.com/questions/553429">question</a> gives some answers, but really I don't understand how it can work since I still want to be able to fetch all childs objects.</p>
  
  <p>Thanks for reading/helping.</p>
  
  <p><strong>PS:</strong> I use hibernate as ORM/JPA provider, so an Hibernate solution can be provided if no JPA solution exists.<br>
  <strong>PS2:</strong> Or just tell me that I should not do this (with arguments of course) ;-)</p>