title: How to write query in JPQL?
body: <p>I have a three classes: Doctor, Patient and Consultation.
  Both Doctor and Patient classes have a list of consultations as field.</p>
  
  <pre><code>@Entity
  public class Consultation {
      @Id
      @GeneratedValue
      private int id;
      private Calendar scheduleDate;
      private String information;
      private String symptoms;
      @ManyToOne
      private Doctor doctor;
      @ManyToOne
      private Patient patient;
  //...
  }
  
  @Entity
  public class Patient {
      @Id
      @GeneratedValue
      private int id;
      private String name;
      private String ssn;
      private String address;
      @OneToMany(mappedBy = "patient")
      private List&lt;Consultation&gt; consultations;
  //...
  }
  
  @Entity
  public class Doctor  {
      @Id
      @GeneratedValue
      private int id;
      private String name;
      private String specialization;
      @OneToMany(mappedBy = "doctor")
      private List&lt;Consultation&gt; consultations;
  //...
  }
  </code></pre>
  
  <p>I want to obtain the patients of a doctor from a single query; that is all the patients that have the same consultation as a doctor. Note that there is no connection between Doctor and Patient.
  Is this posible?</p>
  
  <pre><code>select p from Patient p where p.consultations **someKeyword** (select d.consultations from Doctor d where d.id = :doctorId)
  </code></pre>
  
  <p>If I'm not mistaken <em>someKeyword</em> would be <em>contains</em> if there would be </p>
  
  <pre><code>where list&lt;entity&gt; contains entity
  </code></pre>
  
  <p>and <em>in</em> if</p>
  
  <pre><code>where entity in list&lt;entity&gt;
  </code></pre>
  
  <p>but in this case there would be</p>
  
  <pre><code>list someKeyword list
  </code></pre>
  
  <p>A combination would be:</p>
  
  <pre><code>select p from Patient p where p.consultations contains (select c from Consultation c where c in (select d.consultations from Doctor d where d.id = :doctorId))
  </code></pre>
  
  <p>but does this make sens?</p>
  
  <p>I am a beginner in JPA and JPQL.</p>