gpt4 book ai didi

java - 如何用JPQL编写查询?

转载 作者:行者123 更新时间:2023-12-02 00:24:42 25 4
gpt4 key购买 nike

我有三个类(class):医生、病人和咨询。医生和病人类(class)都有一个咨询列表作为字段。

@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将是包含如果有

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 的初学者。

最佳答案

类似于:

select p from Patient p
where exists (
select c from Consultation c
where c.patient = p
and c.doctor.id = :doctorId
)

关于java - 如何用JPQL编写查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10261111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com