Java Hibernate:ManyToMany具有额外的列,不创建关联

Java Hibernate:ManyToMany具有额外的列,不创建关联,java,eclipse,hibernate,jpa,Java,Eclipse,Hibernate,Jpa,我遵循指南,以便在我的两个类医生和表之间创建多对多关联。我希望关联的表有额外的列,因此我需要根据教程执行以下步骤 但是,现在当我运行代码创建关联时,数据库中没有创建任何内容 这是我的密码 医生 package edu.cs157b.hibernate; import java.util.ArrayList; import java.util.List; import javax.persistence.*; @Entity @Table(name="DOCTOR_INFO") @Name

我遵循指南,以便在我的两个类医生之间创建多对多关联。我希望关联的表有额外的列,因此我需要根据教程执行以下步骤

但是,现在当我运行代码创建关联时,数据库中没有创建任何内容

这是我的密码

医生

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;
    }
}
任命请求ID

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

还有一种更好的方法可以访问多对多关系,而不是创建自己的自定义方法,通过任命请求表获得患者的医生的

我认为您需要将AppointmentRequest对象升级为实体并保存patient和doctor对象(级联关联将在同一事务中保存预约请求)。 因为你没有救病人或医生,实际上什么也救不了

下面的答案清楚地解释了嵌入式对象和实体之间的区别


因为预约请求除了映射患者和医生之间的关联外,还包含其他信息,所以它应该是一个实体。

发布您的
hibernate.cfg.xml
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;         
}