如何从连接表中为附加属性创建多对多的Hibernate映射?

时间:2012-04-24 08:28:17

标签: spring hibernate hibernate-mapping

我需要多对多的hibernate映射需要3个连接。我试图找到一个没有像LecturerCourse这样的中间实体的解决方案。

我的讲师和课程表之间的数据库中有很多关系。一个课程可以由几位讲师授课,而讲师可以开设几门课程。

我手头存有课程。但是,我需要为讲师分配课程。当我分配课程时,我也会存储该课程的容量。

我的数据库图表:

enter image description here

我使用hibernate和spring。当课程分配给任何讲师时,我需要一个hibernate映射。我需要在容量字段中添加值。

我的讲师映射:

@Entity
@Table(name="LECTURER")
public class Lecturer {

    @Id 
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="LECTURER_ID_SEQ")
    @SequenceGenerator(name="LECTURER_ID_SEQ", sequenceName="LECTURER_ID_SEQ")
    private Long Id;

    @Column(name="NAME")
    private String name;

    @Column(name="SURNAME")
    private String surname;


    @Column(name="EMAIL")
    private String email;

    @Column(name="USERNAME")
    private String username;

    @Column(name="PASSWORD")
    private String Password;

    @ManyToMany
    @JoinTable(
          name="LECTURER_COURSE",
          joinColumns=@JoinColumn(name="LECTURER_ID"),
          inverseJoinColumns=@JoinColumn(name="COURSE_ID")
      )
    private List<Course> courses;

    //getters - setters
}

我的课程映射:

@Entity
@Table(name="COURSE")
public class Course {

    @Id 
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="COURSE_ID_SEQ")
    @SequenceGenerator(name="COURSE_ID_SEQ", sequenceName="COURSE_ID_SEQ")
    private Long id;

    @Column(name="NAME")
    private String name;

    @Column(name="CODE")
    private String code;
}

知道如何解决我的问题吗?

1 个答案:

答案 0 :(得分:27)

您需要使用@EmbeddedId@Embeddable注释来解决此问题:

讲师课程:

@Entity
@Table(name="LECTURER")
public class Lecturer {

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.lecturer", cascade=CascadeType.ALL)
Set<LecturerCourse> lecturerCourses == new HashSet<LecturerCourse>();

//all others properties Setters and getters are less relevant.

}

课程类:

@Entity
@Table(name="COURSE")
public class Course {

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.course", cascade=CascadeType.ALL)
Set<LecturerCourse> lecturerCourses == new HashSet<LecturerCourse>();

//all others properties Setters and getters are less relevant.

}

LecturerCourse课程:

@Entity
@Table(name = "lecturer_course")
@AssociationOverrides({
        @AssociationOverride(name = "pk.lecturer", 
            joinColumns = @JoinColumn(name = "LECTURER_ID")),
        @AssociationOverride(name = "pk.course", 
            joinColumns = @JoinColumn(name = "COURSE_ID")) })
public class LecturerCourse {

    private LecturerCourseID pk = new LecturerCourseID();

    @Column(name = "CAPACITY", nullable = false, length = 10)
    private String capacity;

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

}

现在是主键:

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

    private Lecturer lecturer;
    private Course course;

    @ManyToOne
    public Stock getLecturer() {
        return lecturer;
    }

    public void setLecturer(Lecturer lecturer) {
        this.lecturer= lecturer;
    }

    @ManyToOne
    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course= course;
    }

}

现在你的主要应该是这样的:

Lecturer lecturer1 = new Lecturer();
Course math = new Course();
LecturerCourse lecturer1math  = new LecturerCourse();
lecturer1math.setCapacity("capacity");
lecturer1math.setLecturer(lecturer1);
lecturer1math.setCourse(math);
lecturer1.getLecturerCourses().add(lecturer1math);

//saving object
session.save(lecturer1);

您需要确保标记为@Embeddable的类应实现Serializable标记界面。

希望它有所帮助。