I am unsuccessfuly trying to create bridge table that would resolve two @ManyToMany relations. However this table have to contain additional field. For example:
Course: -course_id - pk
Student: -student_id -pk
Bridge: -(course_id, student_id) - pk
-additional_field
My student class looks like this:
@Entity
public class Student extends Model {
@Id
@OneToMany
public List<Bridge> student_id;
}
Course class is basicaly the same.
Bridge table looks like this:
@Entity
public class Bridge extends Model{
@EmbeddedId
public compound_key student_course;
public String additional_field;
@Embeddable
public class compound_key{
@ManyToOne
public Student student_id;
@ManyToOne
public Student course_id;
}
}
Thank you for help.
I have found the following solution. This is a solution without a composite key in Bridge. I added normal @Id field in Bridge class and relations to Student and Course are normal relations. This solution contains an additional 'id' field in the 'bridge' table in the database.
Here is the code:
Student.java:
Course.java:
Bridge.java:
EDIT
After many attempts I have found solution with composite key in Bridge class. Classes Student and Course are the same as in previous solution.
Bridge.java changed to following:
And there is additional BridgeId.java:
What is more important in this code is:
Above solution has several workarounds. But I wasn't ableto find easier and cleaner one.