如何为连接表中的附加属性创建多对多 Hibernate 映射? [英] How Do I Create Many to Many Hibernate Mapping for Additional Property from the Join Table?
问题描述
我需要多对多的休眠映射,需要 3 个连接.我试图找到一个没有像 LecturerCourse
这样的中间实体的解决方案.
I need a many to many hibernate mapping needed 3 joins. I've tried to find out a solution without intermediate entity like LecturerCourse
.
我的数据库中有讲师表和课程表之间的多对多关系.一门课程可以由几个讲师讲授,而一个讲师可以讲几门课程.
I have a many to many relation in my database between my lecturer and course tables. A course can be given by several lecturer while a lecturer can give several courses.
我事先存储了课程.但是,我需要为讲师分配课程.当我分配课程时,我还会存储该课程的容量.
I have courses stored before hand. However, I need to assign courses to lecturer. When I assign courses I also store the capacity of that course.
我的数据库图:
我使用休眠和弹簧.当课程分配给任何讲师时,我需要一个休眠映射.我需要为容量字段添加值.
I use hibernate and spring. I need a hibernate mapping when a course is assign any lecturer. I need to add values to capacity field.
我的讲师映射:
@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;
}
知道如何解决我的问题吗?
Any idea how to solve my problem ?
推荐答案
需要使用@EmbeddedId
和@Embeddable
注解来解决这个问题:
You need to use @EmbeddedId
and @Embeddable
annotations to solve this issue:
讲师班:
@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.
}
讲师课程:
@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;
}
}
现在你的 Main 应该是这样的:
now Your Main should be something like this:
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
标记接口.
You need to be sure that class marked as @Embeddable
should implement Serializable
marker interface.
希望有帮助.
这篇关于如何为连接表中的附加属性创建多对多 Hibernate 映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!