Hi everyone,
As the header says, I have a bi-directional, many-to-many self-referential association. The relationship is stored in a join table with a composite primary key of the 2 foreign keys. My hibernate mapping is similar to this example:
Code:
<class name="ProductClass" table="PRODUCT_TABLE">
<id column="PRODUCT_ID".../>
<set name="primaryProducts" lazy="true" table="PRIMARY_ASSOCIATE_PRODUCTS_TABLE">
<key column="PRODUCT_ID"/>
<many-to-many class="ProductClass" column="ASSOCIATE_PRODUCT_ID"/>
</set>
<set name="associateProducts" inverse="true" lazy="true" table="PRIMARY_ASSOCIATE_PRODUCT_TABLE">
<key column="ASSOCIATE_PRODUCT_ID"/>
<many-to-many class="ProductClass" column="PRODUCT_ID"/>
</set>
</class>
So I retrieve an instance of Product, and make it transient:
Code:
ProductClass thisProduct = findById(product_id);
makeTransient(thisProduct);
commit;
My question is, can I expect Hibernate to find & delete all instances of
thisProduct where it is EITHER in the set of
primaryProducts or
associateProducts (i.e., in either column of
PRODUCT_ASSOCIATE_PRODUCT_TABLE)? What's happening now is that thisProduct and all relationships where thisProduct is the primaryProduct are being deleted (the row in PRODUCT_ASSOCIATE_TABLE is deleted). But if thisProduct is the associateProduct, it remains in the PRIMARY_ASSOCIATE_PRODUCT_TABLE and is now orphaned, because the primary key in PRODUCT_TABLE is no longer there.
That does seem like a lot of logic to expect from Hibernate, but how is it even able to delete the PRODUCT_TABLE entry if there are still references to it as an associateProduct? I would think that a cascade option would be necessary, but the docs says that isn't needed for many-to-many relationships.
Thanks for any input.