I hope it is okay to ask for help here :)
would like to ask for help on my hibernate project
I have 3 tables: student, class and student_class student_class is a joint table for student and class
the following is the setup:
Student.java @ManyToMany( targetEntity=Class.class, cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER ) @JoinTable( name="student_class", joinColumns=@JoinColumn(name="student_id"), inverseJoinColumns=@JoinColumn(name="class_id") ) @Fetch(value = FetchMode.SUBSELECT) public Set<Class> classes;
_____ Class.java @ManyToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, targetEntity = Student.class ) @JoinTable( name="student_class", joinColumns=@JoinColumn(name="class_id"), inverseJoinColumns=@JoinColumn(name="student_id") ) public Set<Student> students;
What I want to do is when the student changes one of its classes the joint table (student_class) should be updated. But this is not happening in my current implementation.
for example Student A has Math and Science classes. This creates 2 records in the joint table. Now Student A decides to change Science class to French class. In my assumption, this should delete the A-and-Science record and inserts the A-and-French record in the joint table. But the A-and-Science record is not deleted that results to 3 classes for Student A.
How do I implement the correct mapping in order for A-and-Science to be deleted?
Thanks for the help. rein
|