繁体   English   中英

休眠OneToOne关系

[英]Hibernate OneToOne relationship

我有3个类别的约会,患者和医生。约会与患者和医生都有1对1换名。 当我每次在数据库中插入约会对象时,新的患者和医生对象也会插入数据库中。

患者类别:

@Entity
@Table(name = "Patient")
public class Patient {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int patientId;
private String firstName;
private String lastName;
private int age;
private String cnic;
private String contactNumber;
private String homeNumber;
private String country;
private String city;
private String town;
private String streetNo;
private String houseNo;
private String email;
private String username;
private String password;

public int getPatientId() {
    return patientId;
}
public void setPatientId(int patientId) {
    this.patientId = patientId;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public String getCnic() {
    return cnic;
}
public void setCnic(String cnic) {
    this.cnic = cnic;
}
public String getContactNumber() {
    return contactNumber;
}
public void setContactNumber(String contactNumber) {
    this.contactNumber = contactNumber;
}
public String getHomeNumber() {
    return homeNumber;
}
public void setHomeNumber(String homeNumber) {
    this.homeNumber = homeNumber;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getTown() {
    return town;
}
public void setTown(String town) {
    this.town = town;
}
public String getStreetNo() {
    return streetNo;
}
public void setStreetNo(String streetNo) {
    this.streetNo = streetNo;
}
public String getHouseNo() {
    return houseNo;
}
public void setHouseNo(String houseNo) {
    this.houseNo = houseNo;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
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 int getId(){
    return patientId;
}

public Patient getPatient(){
    return this;
}
}

医生班:

@Entity
@Table(name = "Doctor")
public class Doctor extends Users {

private String specialization;


public String getSpecialization() {
    return specialization;
}

public void setSpecialization(String specialization) {
    this.specialization = specialization;
}
}

预约班:

@Entity
public class AppointmentClass {

@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int appointmentId;
private int appointmentDay;
private int appointmentTime;
@OneToOne (cascade=CascadeType.ALL,fetch = FetchType.EAGER)
private Patient patient;
@OneToOne (cascade=CascadeType.ALL,fetch = FetchType.EAGER)
private Doctor doctor;
public int getAppointmentId() {
    return appointmentId;
}
public void setAppointmentId(int appointmentId) {
    this.appointmentId = appointmentId;
}
public int getAppointmentDay() {
    return appointmentDay;
}
public void setAppointmentDay(int appointmentDay) {
    this.appointmentDay = appointmentDay;
}
public int getAppointmentTime() {
    return appointmentTime;
}
public void setAppointmentTime(int appointmentTime) {
    this.appointmentTime = appointmentTime;
}
public Patient getPatient() {
    return patient;
}
public void setPatient(Patient patient) {
    this.patient = patient;
}
public Doctor getDoctor() {
    return doctor;
}
public void setDoctor(Doctor doctor) {
    this.doctor = doctor;
}
}

服务等级:

public class AppointmentPatientService {
private SessionFactory sessionFactory = null;

    public AppoinmentPatient createNewAppointment(AppoinmentPatient appointment){

    try{
        sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Patient patient = new Patient();
        Doctor doctor = new Doctor();
        patient = (Patient)(appointment).getPatient();
        AppointmentClass appointment1 = new AppointmentClass();
        appointment1 = (AppointmentClass)(appointment).getAppointment();
        doctor = (Doctor)appointment.getDoctor();
        appointment1.setPatient(patient);
        appointment1.setDoctor(doctor);
        session.beginTransaction();
        session.save(appointment1);
        session.getTransaction().commit();
        session.close();
    }catch(Exception ex){
        ex.printStackTrace();
    }
    return appointment;
}
}

有什么方法可以在我保存约会对象时将患者和医生的新对象不保存到数据库中。 我将很感激:)

在类AppointmentClass ,更改级联设置。 您可以使用cascade=CascadeType.NONE ,这将确保关联的PatientDoctor对象未保存到数据库。

您可以查看CascadeType所有其他值以找到适合您的选择。

我认为您的关系类型不应该是医生或患者的“一对一”关系,因为一位医生可以有很多约会,而一位患者可以有很多约会。 因此,双方应该是OneToMany,在这种情况下,如果您为约会提供正确的现有医生和患者ID,则不会为每个新约会创建新的医生和新的患者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM