简体   繁体   中英

Hibernate OneToOne relationship

I have 3 classes Appointment,Patient and Doctor.Appointment have 1to1 reletionship with both Patient and Doctor. When i insert a appointment object in database everytime the new patient and doctor object is also inserted in the database.

Patient Class:

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

Doctor Class :

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

Appointment Class:

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

Service Class:

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

Is there any way that when i save the appointment object the new objects of patient and doctor not save to the database. I shall be thankful :)

In the class AppointmentClass , change the cascade settings. You can use cascade=CascadeType.NONE , This will make sure that the associated Patient and Doctor objects are not saved to database.

You can see all other values of CascadeType to find the right choice for you.

I think your relationship type should not be OneToOne from neither the doctor or the patient, because one doctor can have many appointments and one patient can have many appointments. So it should be OneToMany from both sides, in which case a new doctor and a new patient won't be created for each new appointment if you supply the appointment with correct existing doctor and patient ID-s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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