Hibernate OneToOne关系

时间:2015-12-21 11:52:38

标签: java hibernate

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

当我保存约会对象时,有没有办法将患者和医生的新对象保存到数据库中。 我将感恩:)

2 个答案:

答案 0 :(得分:0)

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

您可以查看CascadeType的所有其他值,以便为您找到正确的选择。

答案 1 :(得分:0)

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