我有一个关于EJB中ManyToMany设计的问题,jointable如何拥有属性?
举个例子,学生和课程都是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 中有一个属性,例如每个学生的一门课程都有一个分数。如何在 EJB 中使用 ManyToMany 实现它?
非常感谢您的关注!
这是不可能的,你不能在关系中添加财产。如果您需要访问连接表中的属性,那么该属性属于某个实体,因此您需要第三个实体。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)