我有一个三类:医生,病人和咨询。医生和患者课程都有一个咨询列表作为字段。
@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;
//...
}
我想从单个查询中获取医生的患者;那是所有与医生进行相同咨询的患者。请注意,医生和患者之间没有任何联系。这可能吗?
select p from Patient p where p.consultations **someKeyword** (select d.consultations from Doctor d where d.id = :doctorId)
如果我没记错的话 someKeyword将是contains如果有的话
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 的初学者。