医師、患者、相談の 3 つのクラスがあります。医師と患者の両方のクラスには、フィールドとして相談のリストがあります。
@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<Consultation> consultations;
//...
}
@Entity
public class Doctor {
@Id
@GeneratedValue
private int id;
private String name;
private String specialization;
@OneToMany(mappedBy = "doctor")
private List<Consultation> consultations;
//...
}
単一のクエリから医師の患者を取得したいと考えています。つまり、医師と同じ診察を受けるすべての患者です。Doctor と Patient の間にはつながりがないことに注意してください。これは可能ですか?
select p from Patient p where p.consultations **someKeyword** (select d.consultations from Doctor d where d.id = :doctorId)
私が間違っていなければ、 someKeywordが含まれている場合
where list<entity> contains entity
そしてもし_
where entity in list<entity>
しかし、この場合は
list someKeyword list
組み合わせは次のようになります。
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))
しかし、これは理にかなっていますか?
私はJPAとJPQLの初心者です。