2015-12-21 152 views
0

我有3类任用,病人和医生。约会与病人和医生1to1 relationship。 当我每次在数据库中插入约会对象时,新的患者和医生对象也被插入到数据库中。休眠一对一关系

Patient类:

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

有什么办法,当我保存约会对象病人和医生的新对象不保存到数据库。 我会感激:)

回答

0

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

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

+0

没有cascade = CascadeType.NONE类型。 – Asad

+0

检查链接https://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/metamodel/binding/CascadeType.html –

0

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

+0

我检查这是为什么每次新对象都存储在数据库中? – Asad

+0

因为这就是OneToOne关系的工作原理。你说一个医生只能有一个约会,一个患者只能约一个约会。但是,如果您将其改正为OneToMany,您将不会发表这样的声明。 – Tarmo

+0

好吧,我明白了。 – Asad