-->
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.  [ 6 posts ] 
Author Message
 Post subject: bidirectional many-to-many update from both ends
PostPosted: Sat Feb 18, 2006 6:40 am 
Beginner
Beginner

Joined: Sat Feb 18, 2006 6:24 am
Posts: 23
HI,

I was reading "Hibernate in Action" about many-to-many bidirectional relationship. It says that this kind of relationship has to have one end which will actualy do the updates to the database when calling saveOrUpdate.

I am wondering why can't Hibernate update the database form both sides?
Or can it?


Top
 Profile  
 
 Post subject: Re: bidirectional many-to-many update from both ends
PostPosted: Sat Feb 18, 2006 6:29 pm 
Newbie

Joined: Thu Dec 01, 2005 11:34 am
Posts: 19
bigor wrote:
HI,

I was reading "Hibernate in Action" about many-to-many bidirectional relationship. It says that this kind of relationship has to have one end which will actualy do the updates to the database when calling saveOrUpdate.

I am wondering why can't Hibernate update the database form both sides?
Or can it?


You can update the database from both ends. Set the cascade="save-update" on both the one and many side of the mapping.

Example:

On the one side, the cascade option can be set for the set of objects:
<set name="foo" inverse="true" cascade="save-update">
...
</set>

On the many side, set the property of the parent to also cascade:
<many-to-one name="bar" class="com.bar" cascade="save-update">
...
</many-to-one>

This way, you can create both foo and bar objects, and cascade save them.

I do this when I have an entity set like this:

Parent Type1 -> Child <- Parent Type2.

I'll take a known parent, add a new child with a new parent type 2, and save the whole thing by calling save on just the type 1 object.

Hope this helps!


Top
 Profile  
 
 Post subject: many-to-many
PostPosted: Sun Feb 19, 2006 3:50 pm 
Beginner
Beginner

Joined: Sat Feb 18, 2006 6:24 am
Posts: 23
My question was about many-to-many relaton, not meny-to-one. With many-to-one it works fine, but many-to-many is the problem.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 19, 2006 8:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
This issue applies if you want the Java Collection updates to be equal to the database sync state. So, in the case of a many-to-many in Java you set both sides of the collection thus mapping has to have one side set as inverse=true so you dont get two updates (which will conflict) to the database. If you are only setting one side of the relationship in Java then no need to have the inverse true on any side - but be carefull as the Java collection will not match the database. In many cases, your throwing the collection away after the unit-of-work so its fine.


Top
 Profile  
 
 Post subject: re
PostPosted: Mon Feb 20, 2006 8:20 am 
Beginner
Beginner

Joined: Sat Feb 18, 2006 6:24 am
Posts: 23
So, if I don't want synchronization with database on every add or remove from my collection, but on the end of the transaction it's Ok to remove inverse="true" from collection mapping on both sides?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 20, 2006 7:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Yes - Experiment a bit and see.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.