title: JPA Query: select all parents that have their last child with a specific property
body: <p>I'm guessing how the query below could be written through JPQL:</p>
  
  <blockquote>
    <p>Select all parents(side 1 of a 1-n relationship) which last child
    (newest id) has a specific value for a column (attribute). My entities look like</p>
  </blockquote>
  
  <p>Parent.java</p>
  
  <pre><code>@Entity
  class Parent {
  
  @Id
  @Column(name="ID_PARENT")
  @GeneratedValue(strategy=GenerationType.IDENTITY) 
  private Long id;
  
  @OneToMany(mappedBy="parent")
  private List&lt;Child&gt; children;
  
  }
  </code></pre>
  
  <p>Child.java</p>
  
  <pre><code>@Entity 
  class Child {
  
  @Id
  @Column(name="ID_CHILD")
  @GeneratedValue(strategy=GenerationType.IDENTITY) 
  private Long id;
  
  
  @ManyToOne()
  @JoinColumn(name="ID_PARENT")
  private Parent parent;
  
  private String attribute;
  
  
  }
  </code></pre>
  
  <p>What would JPQL look like?
  Thank you all</p>