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!