我正在尝试使用复合主键在学生和教学课程之间建立许多联系:
我的课程:
@Entity@Table(name="Student_mtm_cId")public class Student { private String id; private Set<StudentTClass> teachingClasses = new HashSet<StudentTClass>(); @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.student") public Set<StudentTClass> getTeachingClasses() { return teachingClasses; } public void setTeachingClasses(Set<StudentTClass> teachingClasses) { this.teachingClasses = teachingClasses; } public void addStudentToClass(TeachingClass teachingClass){ StudentTClass studentTClass = new StudentTClass(); studentTClass.setStudent(this); studentTClass.setTeachingClass(teachingClass); teachingClasses.add(studentTClass); } public void setLastName(String lastName) { this.lastName = lastName; } @Id @GeneratedValue(generator="system-uuid") @GenericGenerator(name="system-uuid", strategy = "uuid") @Column(name = "student_id", nullable = false) public String getId() { return id; } public void setId(String id) { this.id = id; } //all other setters and getters and isequal/hashCode omitted.}
TeachingClass:
@Entity@Table(name="TechingClass_MTM")public class TeachingClass { private String id; private String name; private String description; private Set<StudentTClass> teachingClasses = new HashSet<StudentTClass>(); public TeachingClass(){} public TeachingClass(String name, String description) { super(); this.name = name; this.description = description; } public void addStudentToClass(Student student){ StudentTClass studentTClass = new StudentTClass(); studentTClass.setStudent(student); studentTClass.setTeachingClass(this); teachingClasses.add(studentTClass); } @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.teachingClass") public Set<StudentTClass> getTeachingClasses() { return teachingClasses; }
德玛西亚99
互换的青春
慕容森