2

在网络服务中,我具有以下功能,可根据患者的电话检索医生及其患者的一些信息:

    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。

我已经为医生和病人设置了课程:

@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;
    }
}

医生 病人 患者关系视图![][3]

在这个网络服务中,响应总是相同的(给定手机号码)。它总是给我 patSys=130 和 patDia=80 这是第二个病人的信息!。网络服务一定有问题,但对我来说一切都好!

4

1 回答 1

1

您需要在此处创建Criteria对象Patient,无需在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;
于 2012-11-02T10:26:54.517 回答