Ho una domanda per progettare ManyToMany in EJB, come può un jointable avere una proprietà?
Ecco un esempio, gli studenti ei corsi sono ManyToMany, ogni studente ha molti corsi e molti studenti scelgono un corso.JPA ManyToMany, in che modo JoinTable può avere una proprietà?
@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;
}
}
Tuttavia, se ho una proprietà nel JoinTable, ad esempio ogni studente ha un punteggio per un corso. Come posso farlo in EJB con ManyToMany?
Mille grazie per la vostra attenzione!
Se la vostra relazione ha proprietà, allora si dovrebbe modellare come entità. Questa domanda è quasi identica: http://stackoverflow.com/questions/7602386/mapping-value-in-junction-table-to-entity/7603036#7603036 Anche il nome dell'entità che rappresenta la relazione (CourseAssignment) si adatta abbastanza bene al tuo Astuccio. –
Non è possibile, non è possibile aggiungere proprietà alla relazione. Se è necessario accedere alla proprietà nella tabella di join, la proprietà appartiene a qualche entità e, di conseguenza, è necessaria la terza entità. –
scusate, l'ho corretto. Ho solo un'altra domanda, quando ho usato una classe @Embeddable come PK per jointable, non funziona (compilazione fallita), qualcuno ha detto che JPA2.0 non lo supporta! per favore guarda questo [jpa-eclipselink-manytomany-with-dat] (http://stackoverflow.com/questions/4013397/jpa-eclipselink-manytomany-with-data), c'è un modo per farlo funzionare? – seaguest