私はEJBのManyToManyを設計するための質問があります。ジョイント可能にはどのようにプロパティがありますか?
ここでは、学生とコースはManyToManyであり、すべての学生が多くのコースを持っており、多くの学生が1つのコースを選択しています。JPA ManyToMany、JoinTableにはどのようにプロパティがありますか?
@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にプロパティを持っている場合、たとえば各学生は1つのコースに対して1つのスコアを持っています。 ManyToManyを使用してEJBでどのように作成できますか?
ご注意いただきありがとうございます!
あなたの関係が財産を持っている場合、あなたはエンティティとしてそれをモデル化する必要があります。この質問はほぼ同じです:http://stackoverflow.com/questions/7602386/mapping-value-in-junction-table-to-entity/7603036#7603036関係を表すエンティティの名前(CourseAssignment)はあなたの場合。 –
リレーションシップにプロパティを追加することはできません。結合表のプロパティーにアクセスする必要がある場合、そのプロパティーはあるエンティティーに属し、その結果、第3のエンティティーが必要です。 –
申し訳ありませんが、私はそれを修正しました。私は@Embeddableクラスをジョイント可能なPKとして使用しても機能しません(コンパイルに失敗しました)、JPA2.0はそれをサポートしていないと言いました。この[jpa-eclipselink-manytomany-with-dat](http://stackoverflow.com/questions/4013397/jpa-eclipselink-manytomany-with-data)を見てください。それを動作させる方法はありますか? – seaguest