-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Add element in ManyToMany List results in join table cleaned
PostPosted: Tue Aug 23, 2011 10:30 am 
Newbie

Joined: Mon Dec 10, 2007 7:03 am
Posts: 14
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.