I have a many-to-many mapping between entities course and student with many-to-many table named as student_course.Class student has a getter/setter for the collection of course(s) and vice versa.When I update student after removing a course from the collection(collection of courses) in class student,i see that the following queries are executed on student_course table: (eg:a student had 3 courses and 1 course is removed from the collection)
delete from student_course where student_id =? insert into student_course values(?,?) insert into student_course values(?,?)
Why is there a deletion of all the records and then insertion of two records for the records that are remaining in the collection?Is there a way to delete only the record that was removed from the collection instead of deleting all the courses related to the student ?
Not positive, but it kind of sounds like your mapping is uni-directional, or that you need to remove objects from each Collection on both sides in order for Hiberate to know both ids to include in that first delete and therefore know the one specific record to delete.
So Hibernate in its thinking thinks it is faster to delete all the records, then go through the one side Collection and insert then back into the database.
Just a theory here.
What is your mapping for both sides of the Many to Many?