gpt4 book ai didi

java - 奇怪的 mysql 检索与 hibernate 条件

转载 作者:行者123 更新时间:2023-11-29 03:07:27 24 4
gpt4 key购买 nike

在网络服务中,我有以下功能可以根据病人的电话检索医生和他的病人的一些信息:

    public String findDoctorOfPatient(String phone) {
AnnotationConfiguration config = new AnnotationConfiguration();
config.configure("hibernate.cfg.xml");
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();

session.beginTransaction();

Criteria query = session.createCriteria(doctor.class);
query.createCriteria("patients", "p");
query.add(Restrictions.eq("p.phone", phone));
List<doctor> doctorList = (ArrayList<doctor>) query.list();
session.getTransaction().commit();
String answear = "";
for (doctor d : doctorList) {
answear = answear.concat("docPhone" + d.getPhone() + "docEmail"
+ d.getEmail() + "patDia"
+ d.getPatients().iterator().next().getDiastolic()
+ "patSys"
+ d.getPatients().iterator().next().getSystolic());
}
if (doctorList.isEmpty()) {
session.close();
factory.close();
return "No Doctor!";
} else {
session.close();
factory.close();
return answear;
}
}

问题是当我有一个病人时没问题,但是当我添加第二个病人时它会给我最后一个病人的详细信息,尽管我已经设置了第一个病人电话的标准!

我有 2 个表:

1.doctor(id,username,password,phone,email)2.patient(id,name,surname,phone,systolic,diastolic,doctorid(FK指doctor.id))

我已经正确配置了 hibernate.cfg.xml。

我已经为医生和病人设置了类(class):

@Entity
public class doctor {
@Id
private int id;
private String username;
private String password;
private String phone;
private String email;

@OneToMany(targetEntity = patient.class, cascade = CascadeType.ALL, mappedBy = "doctor")
@Cascade(value = org.hibernate.annotations.CascadeType.ALL)
private Collection<patient> patients = new ArrayList<patient>();

public Collection<patient> getPatients() {
return patients;
}

public void setPatients(Collection<patient> patients) {
this.patients = patients;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}

@Entity
public class patient {
@Id
private int id;
private String name;
private String surname;
private String phone;
private int systolic;
private int diastolic;

@ManyToOne
private doctor doctor;

public doctor getDoctor() {
return doctor;
}

public void setDoctor(doctor doctor) {
this.doctor = doctor;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public int getSystolic() {
return systolic;
}

public void setSystolic(int systolic) {
this.systolic = systolic;
}

public int getDiastolic() {
return diastolic;
}

public void setDiastolic(int diastolic) {
this.diastolic = diastolic;
}
}

doctor patient patientRelationView![][3]

在此网络服务中,响应始终相同(给定手机号码)。它总是给我 patSys=130 和 patDia=80,这是第二个患者的信息!网络服务中一定有问题,但对我来说似乎一切正常!

最佳答案

你需要在Patient上创建Criteria对象,这里不需要获取transaction

Criteria query = session.createCriteria(Patient.class)
.add(Restrictions.eq("phone", phone));

List<Patient> patList = (ArrayList<Patient>) query.list();

String result="";
if(!patList.isEmpty()) {
Patient patient=patList.get(0);
result="Doc Phone : " + patient.getDoctor().getPhone();
}
return result;

关于java - 奇怪的 mysql 检索与 hibernate 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13193062/

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