title: JPQL Query - get child item in list from a parent
body: <p>How would I create a JPQL query to get a <strong>Record</strong> if it exists in a <strong>Folder</strong> when I have the <strong>Folder</strong> <em>:id</em> and the <strong>Record</strong> <em>:refId</em> (named parameters). I would like to have the <strong>Record</strong> returned back from the query as there is other data that is part of the <strong>Record</strong> needed.</p>
  
  <p>Here are the entities:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity
  public class Folder {
      @Id
      @Column(name = "id")
      private Long id;
  
      @OneToMany(targetEntity = Record.class, cascade = { CascadeType.ALL })
      @JoinColumn(name = "RECORD_FOLDER_0")
      @OrderBy("id ASC")
      private List&lt;Record&gt; records;
      ...
  }
  
  @Entity
  public class Record {
      @Id
      @Column(name = "id")
      private Long id;
  
      @Basic
      @Column(name = "refId")
      private Long refId;
  
      @Basic
      @Column(name = "data", length = 255)
      private String data;
      ...
  }
  </code></pre>
  
  <p>The regular SQL would look something like:</p>
  
  <pre class="lang-java prettyprint-override"><code>.createNativeQuery(String.format("SELECT * FROM record r WHERE r.refId=%d AND r.RECORD_FOLDER_0=%d", refId, folderId));
  </code></pre>
  
  <p>I am struggling on how to use a JPQL query to do this.</p>