• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

many-to-many mapping

 
Venkatesh Kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:

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 ?

Thanks for all your responses!!!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Venkatesh Kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark:

Thanks for your reply.I have many-to-many on both the sides.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic