我有一个关于在 EJB 中设计 ManyToMany 的问题,可连接对象如何具有属性?
这里举个例子,学生和课程是ManyToMany,每个学生都有很多课程,很多学生选择一门课程。
@Entity
public class Student implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
private Collection<Course> courses;
@ManyToMany(mappedBy = "students",cascade=CascadeType.ALL)
public Collection<Course> getCourses() {
return this.courses;
}
public void setCourses(Collection<Course> courses) {
this.courses = courses;
}
}
@Entity
public class Course implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
private Collection<Student> students;
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name = "Student_Course",
joinColumns = {@JoinColumn(name = "Course_ID", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "Student_ID", referencedColumnName = "id")})
public Collection<Student> getStudents() {
return this.students;
}
public void setStudents(Collection<Student> students) {
this.students = students;
}
}
但是,如果我在 JoinTable 中有一个属性,例如每个学生有一门课程的分数。如何使用 ManyToMany 在 EJB 中实现它?
非常感谢您的关注!