用hibernate标准检索奇怪的mysql

时间:2012-11-02 10:03:57

标签: java mysql hql hibernate-criteria

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

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

问题是,当我有一名患者没事时,但是当我添加第二名患者时,它给了我最后一名患者的详细信息,尽管我已经设定了firt患者电话的标准!

我有两张桌子:

1.doctor(ID,用户名,密码,电话,电子邮件) 2.病人(身份证,姓名,姓名,电话,收缩期,舒张期,医生(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;
    }
}

doctor patient patientRelationView![][3]

在此网络服务中,响应始终相同(给定移动电话号码)。 它总是给我patSys = 130和patDia = 80这是第二个患者信息!。网络服务中的一些必须是错的,但对我来说一切似乎都没问题!

1 个答案:

答案 0 :(得分: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;