Hello,
Given a many to many relation from user to group:
Code:
@ManyToMany
private List<Group> groups=new ArrayList<Group>();
public void addGroup(Group group) {
this.groups.add(group);
}
I had a group to the user:
Code:
User user=(User) session.get(User.class, 123);
Group group=(Group) session.get(Group.class, 1);
user.addGroup(group);
I get SQL
Code:
select ... from User where id=123
select ... from Group where id=1
select ... from User_Group ug inner join Group g on ug.group_id=g.id where ug.user_id=123
update User set ...
insert into User_Group (user_id,group_id) values (123,1)
Well... everything's OK until now. Then I repeat the operation another time (different transaction):
Code:
User user=(User) session.get(User.class, 123);
Group group=(Group) session.get(Group.class, 2);
user.addGroup(group);
I get SQL
Code:
select ... from User where id=123
select ... from Group where id=2
select ... from User_Group ug inner join Group g on ug.group_id=g.id where ug.user_id=123
delete from User_Group where user_id=123
insert into User_Group (user_id,group_id) values (123,1)
insert into User_Group (user_id,group_id) values (123,2)
I wonder why user groups are being deleted and reinserted before the second insertion?
Either when I replace the List<Group> by a Set<Group> or when I had a @OrderColumn, this strange behaviour doesn't occur anymore. I would have expected the contrary.
Can someone explain me the reason for this behaviour?