[ACCEPTED]-Can add extra field(s) to @ManyToMany Hibernate extra table?-hibernate
If you add extra fields on a linked table 13 (STUDENT_COURSE), you have to choose an 12 approach according to skaffman's answer 11 or another as shown bellow.
There is an approach 10 where the linked table (STUDENT_COURSE) behaves 9 like a @Embeddable according to:
@Embeddable
public class JoinedStudentCourse {
// Lets suppose you have added this field
@Column(updatable=false)
private Date joinedDate;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="STUDENT_ID", insertable=false, updatable=false)
private Student student;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="COURSE_ID", insertable=false, updatable=false)
private Course course;
// getter's and setter's
public boolean equals(Object instance) {
if(instance == null)
return false;
if(!(instance instanceof JoinedStudentCourse))
return false;
JoinedStudentCourse other = (JoinedStudentCourse) instance;
if(!(student.getId().equals(other.getStudent().getId()))
return false;
if(!(course.getId().equals(other.getCourse().getId()))
return false;
// ATT: use immutable fields like joinedDate in equals() implementation
if(!(joinedDate.equals(other.getJoinedDate()))
return false;
return true;
}
public int hashcode() {
// hashcode implementation
}
}
So you will 8 have in both Student and Course classes
public class Student {
@CollectionOfElements
@JoinTable(
table=@Table(name="STUDENT_COURSE"),
joinColumns=@JoinColumn(name="STUDENT_ID")
)
private Set<JoinedStudentCourse> joined = new HashSet<JoinedStudentCourse>();
}
public class Course {
@CollectionOfElements
@JoinTable(
table=@Table(name="STUDENT_COURSE"),
joinColumns=@JoinColumn(name="COURSE_ID")
)
private Set<JoinedStudentCourse> joined = new HashSet<JoinedStudentCourse>();
}
remember: @Embeddable 7 class has its lifecycle bound to the owning 6 entity class (Both Student and Course), so 5 take care of it.
advice: Hibernate team suppports 4 these two approachs (@OneToMany (skaffman's 3 answer) or @CollectionsOfElements) due some 2 limitations in @ManyToMany mapping - cascade 1 operation.
regards,
The student_course table is there purely 9 to record the association between the two 8 entities. It is managed by hibernate, and 7 can contain no other data.
The sort of data 6 you want to record needs to be modelled 5 as another entity. Perhaps you could a one-to-many 4 association between Course and StudentResult 3 (which contains the grade, etc), and then 2 a many-to-one association between StdentResult 1 and Student.
Drop the many-to-many, create a class called 5 StudentCourseRelationship and set up one 4 to manys on Student and Course to the StudentCourseRelationship.
You 3 can put all sorts of things on it, like 2 DateEnrolled, DateKickedOut etc. etc.
IMO 1 the many-to-many mapping is a bit of a con.
The accepted answer unfortunately doesn't 5 work for me, hibernate generates the join 4 table in a weird way (all join columns are 3 duplicated). However the variant with dedicated 2 entity for the join table works fine. Here 1 it is described in great detail: http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.