Issue
Is there a way I can join intermediate table in the ManyToMany
table. You can consider the example given at https://dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html
As an example in the above Student
to Course
many to many relation, we can have tables STUDENT
, COURSE
and STUDENT_COURSE
.
My question is to check if there is any way we can provide this in hibernate in java so that Student
class has mapping for STUDENT_COURSE
intermediate table instead of COURSE
table?
I want to modify below class to get the above output:-
@Entity
@Table(name = "STUDENT")
public class Student {
private long studentId;
private String studentName;
private Set<Course> courses = new HashSet<Course>(0);
public Student() {
}
public Student(String studentName) {
this.studentName = studentName;
}
public Student(String studentName, Set<Course> courses) {
this.studentName = studentName;
this.courses = courses;
}
@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
public long getStudentId() {
return this.studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
@Column(name = "STUDENT_NAME", nullable = false, length = 100)
public String getStudentName() {
return this.studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "STUDENT_COURSE",
joinColumns = { @JoinColumn(name = "STUDENT_ID") },
inverseJoinColumns = { @JoinColumn(name = "COURSE_ID") })
public Set<Course> getCourses() {
return this.courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
}
Solution
Short answer:
you should divide your many to many association to two many to one association and use @Embedded annotation to define compound key for your intermediate entity look at below link for detail
Long answer:
Actually when you use @ManyToMany annotation to establish many to many association in hibernate it creates another table to establish ManyToMany association behind the scene this table is defined with @JoinTable annotation in your code, query to that table is completely managed by hibernate to achieve many to many association and you don't have entity which is mapped to that table. It is fine unless you need extra column in your intermediate table(here STUDENT_COURSE) or you want to execute some specific query to that table in a way that you behave with your other entity and you want to access the Id of your Course and Student table in CourseStudent table. because when hibernate creates intermediate table behind the scene you don't have entity in your program that maps to that table and therefore you can't have other column for that table and you can't behave to that table as an entity in your program. if you need extra column you can divide your @ManyToMany association to two @ManyToOne association. Here you should have another entity with name StudentCourse which has two @ManyToOne association one to Student entity and the other one to Course entity. However when you are defining your many to many association this way you should consider data integriry for instance the combination of student_id and course_id in your student_course table is unique and actually it should be a compound key in your student_course table. Fortunately there are some annotations in hibernate and jpa that provide this capability. Writing all the code for this sitiuation in this answer is redundant because there is already a good example in the link below .
Answered By - Tashkhisi
Answer Checked By - Marie Seifert (JavaFixing Admin)