【发布时间】:2012-05-02 22:01:14
【问题描述】:
我有三个课程:医生、患者和咨询。 Doctor 和 Patient 类都有一个咨询列表作为字段。
@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 的初学者。
【问题讨论】:
-
我找到了解决办法;我得到了所有的咨询,然后是每次咨询的病人。不过,我想知道上面的内容是否有意义/是否可行。