Problem I have is more of an approach question than how to use, please let me explain:
Two classes, Student and Homework. Student class has a list of their many homework assignments. Homework class has a list of its many students to complete it.
Some example code just in case someone searches for a quick Many to many example:
@Entity(name="STUDENT")
@Inheritance(strategy=InheritanceType.JOINED)//Note this join type, no redundancy
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@ManyToMany(mappedBy="studentList")//Note how this is mapped
private Collection<Homework> homeworkList = new ArrayList<Homework>();
@Entity(name="HOMEWORK")
@Inheritance(strategy=InheritanceType.JOINED)
public class Homework {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@ManyToMany
private Collection<Student> studentList = new ArrayList<Student>();
Hibernate does its thing and creates a table, student_homework using primary keys to map each homework assignment to students and vice versa.
How could I possibly use Hibernate to save a 'homeworkCompleted' boolean for example. This value cannot exist in the student class as there are many jobs, the same with the homework class as there are many students.
One possible solution would be to possibly manually go into the db, create another column and each time an entry is saved have the option to update the completed or do it sometime later.
I can't help but think there is a very very simple solution out there that hibernate can take care of and make everything ok, with a little applied logic as well.
Many thanks in advance, Dean
You need two many-to-one now and your student_homework becomes something like Assignment[completed].
More on this here: Mapping many-to-many association table with extra column(s)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With