簡體   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