Hibernate version: 2.1
Mapping documents:
Code:
<class name="Category" table="CATEGORY">
...
<list name="items"table="CATEGORY_ITEM" lazy="true" cascade="save-update">
<key column="CATEGORY_ID"/>
..
<many-to-many class="Item" column="ITEM_ID"/>
</set>
</class>
<class name="Item" table="ITEM">
...
<bag name="categories" table="CATEGORY_ITEM" lazy="true" inverse="true" cascade="save-update">
<key column="ITEM_ID"/>
<many-to-many class="Item" column="CATEGORY_ID"/>
</bag>
</class>
The mapping above is similar to p. 227 of "the bible". Notice that no cascade="delete" is set, as I simply want to disassociate Categories and Items, from both ends, and not delete in cascade.
If I delete a Category referred by an Item, the mn table CATEGORY_ITEM is updated ok, and no exception is thrown.
But if I delete an Item which is referred by a Category, the database throws a foreign key reference violation.
I found a workaround: simply do not put
Code:
inverse="true"
in the mapping, but the all the rest as in the inverse case: this way the mn table CATEGORY_ITEM is updated two ways
(but in this case the primary keys created by the schema tool on the mn table CATEGORY_ITEM may be the wrong ones, as the mapping order becomes relevant).
I wonder whether there is another way to do this, as this is clearly a trick.