-->
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.  [ 5 posts ] 
Author Message
 Post subject: @ManyToMany insertion behaviour
PostPosted: Mon Nov 27, 2006 2:53 pm 
Newbie

Joined: Mon Nov 27, 2006 2:11 pm
Posts: 6
Hi all, I'm using EJB3 with its JPA through jboss-4.0.5.GA and it's Hibernate implementation. The experience has been quite good in general, but when trying to insert a record in a @ManyToMany relation an extrange behaviour happens:

Supose A has many B's (and viceversa, but the navigation is A -> B), so If I want to add one B, let's say b, to A's B collection, I would expect to use a.getBs().add(b). The perplexing situation is that Hibernate first deletes all records in a.getBs(), and then inserts all of them again plus the new b record (as if I would had used a.setBs(), in which case is logical). The problem is that in most situations these records can't be deleted due to DB constraints (i.e. referential integrity) so the simple insertion fails.

I'm sure I must be doing something wrong due to my little JPA/Hibernate experience because it does not make sense to me to have to delete when what I really want is to insert!

At the moment the solution was to write an old fashioned SQL navive insert query, not the idea...

Any help would be appreciated,
Leonardo


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 1:54 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
In case you are using a 'List' (without an index column), use a 'Set' in its place.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 7:19 pm 
Newbie

Joined: Mon Nov 27, 2006 2:11 pm
Posts: 6
I really don't understand your answer, using set all registries are deleted and the new ones inserted, not the desired behaviour of not touching the old ones and adding the new.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 2:13 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
If you use a List without an index column, it maps to a 'Bag' which has the following behaviour :-


"Bags are the worst case. Since a bag permits duplicate element values and has no index column, no primary key may be defined. Hibernate has no way of distinguishing between duplicate rows. Hibernate resolves this problem by completely removing (in a single DELETE) and recreating the collection whenever it changes. This might be very inefficient"
-Hibernate reference doc.


With Set it should have worked fine. Post your P0JOs.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 2:03 pm 
Newbie

Joined: Mon Nov 27, 2006 2:11 pm
Posts: 6
Thank you very much, using Set as you stated solved the problem.


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

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.