2014-02-27 53 views
0

我遵循指導,以創建我的兩個類之間的多對多關聯醫生。我希望關聯的表具有額外的列,因此我需要根據教程執行以下步驟。休眠:ManyToMany與額外的列不創建協會

http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

但是,現在當我跑我的代碼建立關聯,沒有什麼是在數據庫中創建。

這裏是我的代碼

醫生

package edu.cs157b.hibernate; 

import java.util.ArrayList; 
import java.util.List; 

import javax.persistence.*; 

@Entity 
@Table(name="DOCTOR_INFO") 
@NamedQueries (
    { 
     @NamedQuery(name = "Doctor.getAll", query = "from Doctor"), 
     @NamedQuery(name = "Doctor.findByName", query = "from Doctor where name = :name") 
    } 
) 
public class Doctor implements Person { 

    private int id; 
    private String name; 
    private Specialty specialty; 
    private List<AppointmentRequest> appointmentRequests = new ArrayList<AppointmentRequest>(); 

    @Id 
    @GeneratedValue 
    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    @Column(unique=true) 
    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @ManyToOne (fetch = FetchType.EAGER, cascade= CascadeType.PERSIST) 
    @JoinColumn(name="specialty_id") 
    public Specialty getSpecialty() { 
     return specialty; 
    } 

    public void setSpecialty(Specialty specialty) { 
     this.specialty = specialty; 
    } 

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.doctor") 
    public List<AppointmentRequest> getAppointmentRequests() { 
     return this.appointmentRequests; 
    } 

    public void setAppointmentRequests(List<AppointmentRequest> appointmentRequests) { 
     this.appointmentRequests = appointmentRequests; 
    } 

    @Transient 
    public List<Patient> getPatients() { 
     List<Patient> patients = new ArrayList<Patient>(); 

     for(AppointmentRequest appointment:appointmentRequests) { 
      patients.add(appointment.getPatient()); 
     } 
     return patients; 
    } 
} 

患者

package edu.cs157b.hibernate; 

import java.util.ArrayList; 
import java.util.List; 

import javax.persistence.*; 

@Entity 
@Table(name="PATIENT_INFO") 
@NamedQueries (
    { 
     @NamedQuery(name = "Patient.getAll", query = "from Patient"), 
     @NamedQuery(name = "Patient.findByName", query = "from Patient where name = :name") 
    } 
) 
public class Patient implements Person { 

    private int id; 
    private String name; 
    private String medical_record; 
    private List<AppointmentRequest> appointmentRequests = new ArrayList<AppointmentRequest>(); 

    public String getMedical_record() { 
     return medical_record; 
    } 

    public void setMedical_record(String medical_record) { 
     this.medical_record = medical_record; 
    } 

    @Id 
    @GeneratedValue 
    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    @Column(unique=true) 
    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.patient", cascade=CascadeType.ALL) 
    public List<AppointmentRequest> getAppointmentRequests() { 
     return this.appointmentRequests; 
    } 

    public void setAppointmentRequests(List<AppointmentRequest> appointmentRequests) { 
     this.appointmentRequests = appointmentRequests; 
    } 

    @Transient 
    public List<Doctor> getDoctors() { 
     List<Doctor> doctors = new ArrayList<Doctor>(); 

     for(AppointmentRequest appointment:appointmentRequests) { 
      doctors.add(appointment.getDoctor()); 
     } 
     return doctors; 
    } 
} 

AppointmentRequest

package edu.cs157b.hibernate; 

import java.util.Date; 

import javax.persistence.AssociationOverride; 
import javax.persistence.AssociationOverrides; 
import javax.persistence.Column; 
import javax.persistence.EmbeddedId; 
import javax.persistence.Entity; 
import javax.persistence.JoinColumn; 
import javax.persistence.Table; 
import javax.persistence.Temporal; 
import javax.persistence.TemporalType; 
import javax.persistence.Transient; 

@Entity 
@Table(name = "APPOINTMENT_REQUEST") 
@AssociationOverrides({ 
     @AssociationOverride(name = "pk.patient", 
      joinColumns = @JoinColumn(name = "PATIENT_ID")), 
     @AssociationOverride(name = "pk.doctor", 
      joinColumns = @JoinColumn(name = "DOCTOR_ID")) }) 
public class AppointmentRequest implements java.io.Serializable { 

    private AppointmentRequestId pk = new AppointmentRequestId(); 
    private Date appointmentDate; 
    private boolean fulfilled; 

    public AppointmentRequest() { 
    } 

    @EmbeddedId 
    public AppointmentRequestId getPk() { 
     return pk; 
    } 

    public void setPk(AppointmentRequestId pk) { 
     this.pk = pk; 
    } 

    @Transient 
    public Patient getPatient() { 
     return getPk().getPatient(); 
    } 

    public void setPatient(Patient patient) { 
     getPk().setPatient(patient); 
    } 

    @Transient 
    public Doctor getDoctor() { 
     return getPk().getDoctor(); 
    } 

    public void setDoctor(Doctor doctor) { 
     getPk().setDoctor(doctor); 
    } 

    @Temporal(TemporalType.DATE) 
    @Column(name = "APPOINTMENT_DATE", nullable = false, length = 10) 
    public Date getAppointmentDate() { 
     return this.appointmentDate; 
    } 

    public void setAppointmentDate(Date appointmentDate) { 
     this.appointmentDate = appointmentDate; 
    } 

    public boolean equals(Object o) { 
     if (this == o) 
      return true; 
     if (o == null || getClass() != o.getClass()) 
      return false; 

     AppointmentRequest that = (AppointmentRequest) o; 

     if (getPk() != null ? !getPk().equals(that.getPk()) 
       : that.getPk() != null) 
      return false; 

     return true; 
    } 

    public boolean isFulfilled() { 
     return fulfilled; 
    } 

    public void setFulfilled(boolean fulfilled) { 
     this.fulfilled = fulfilled; 
    } 

    public int hashCode() { 
     return (getPk() != null ? getPk().hashCode() : 0); 
    } 
} 

AppointmentRequestId

package edu.cs157b.hibernate; 

import javax.persistence.Embeddable; 
import javax.persistence.ManyToOne; 

@Embeddable 
public class AppointmentRequestId implements java.io.Serializable { 

    private Patient patient; 
    private Doctor doctor; 

    @ManyToOne 
    public Patient getPatient() { 
     return patient; 
    } 

    public void setPatient(Patient patient) { 
     this.patient = patient; 
    } 

    @ManyToOne 
    public Doctor getDoctor() { 
     return doctor; 
    } 

    public void setDoctor(Doctor doctor) { 
     this.doctor = doctor; 
    } 

    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     AppointmentRequestId that = (AppointmentRequestId) o; 

     if (patient != null ? !patient.equals(that.patient) : that.patient != null) return false; 
     if (doctor != null ? !doctor.equals(that.doctor) : that.doctor != null) 
      return false; 

     return true; 
    } 

    public int hashCode() { 
     int result; 
     result = (patient != null ? patient.hashCode() : 0); 
     result = 31 * result + (doctor != null ? doctor.hashCode() : 0); 
     return result; 
    } 

} 

代碼來創建協會

public AppointmentRequest createAppointment(Patient patient, Doctor doctor) { 
    Session session = sessionFactory.openSession(); 
    AppointmentRequest appointment = new AppointmentRequest(); 
    try { 
     session.beginTransaction(); 
     if(patient == null) { 
      throw new NullPointerException(); 
     } 
     if(doctor == null) { 
      throw new NullPointerException(); 
     } 

     appointment.setPatient(patient); 
     appointment.setDoctor(doctor); 
     appointment.setFulfilled(true); 

     patient.getAppointmentRequests().add(appointment); 
     doctor.getAppointmentRequests().add(appointment); 

     session.save(appointment); 
     session.getTransaction().commit(); 
    } 
    finally { 
      session.close(); 
    } 
    return appointment;   
} 

也就是有沒有更好的方式來獲得訪問不必創建自己的許多一對多的關係定製方法獲得病人的醫生通過約會請求表。

+0

將您的'hibernate.cfg.xml' – Yogesh

回答

0

我相信你需要將你的AppointmentRequest對象提升到一個實體並保存患者和醫生對象(級聯關聯會將約會請求保存在同一事務中)。 由於您沒有保存病人或醫生,實際上沒有任何保存。

下面是一個解釋清楚嵌入對象和實體之間的差異的答案。

Why do we use @Embeddable In Hibernate

由於預約請求中攜帶除了剛剛映射病人和醫生它理應是一個實體之間的關聯的其它信息。