JPA ManyToMany,JoinTable怎么会有属性呢?
我有一个关于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 实现它?
非常感谢您的关注!
I have a question for designing the ManyToMany in EJB, how can a jointable has a property?
Here is an example, the students and courses are ManyToMany, every student have many courses, and many students choose one course.
@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;
}
}
However if I have a property in the JoinTable, for example each student has one score for one course. How can I make it in EJB with ManyToMany?
Many thanks for your attention!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是不可能的,你不能将财产添加到关系中。如果您需要访问连接表中的属性,那么该属性属于某个实体,因此您需要第三个实体。
It is not possible, you cannot add property to relationship. If you need to access property in the join table, then that property belongs to some entity and as a result you need third entity.
这是可能的。
您只需通过第三个实体将多对多映射替换为一对多和多对一映射的显式组合,这将表示两个主要实体(示例中的学生和课程)之间的关联)。
请在此处阅读详细信息
It is possible.
You just need to replace many-to-many mapping with the explicit combination of one-to-many and many-to-one mappings via a 3rd entity, that would represent the association between the two primary entities (student and course in your example).
Please read details here
具有多对多关系的实体(商家和服务)。
这可以使用第三方实体来实现,如下所示:-
Entities with Many to Many Relationships (Merchant and Service).
This can be achieved using a third entity as follow:-