My Db is throwing a Constraing violation (FK) because Hibernate performs a cascade delete in the wrong order.
Details: I delete a Member, that has a Wallet with Wallet transactions (value-type), and the wallet transaction has an association to a product just like the Member contains collection of products (see the Hibernate mapping below).
I delete a Member instance, and want Hibernate to remove both the products and Wallet transactions. It seems that it first removes the product instances (through cascading), such that a FK violation is thrown by the DB, as it's still referenced by a Wallet transaction, that wasn't removed yet (through cascade)
I played around with the cascade setting, like all-delete-orphan (on the products), etc..., but no luck I also emptied the wallet transactions and flushed the hibernate session in the same removal transaction, but also the same error.
Please some understanding and help to get the order of cascade deletion correct?
The hibernate mapping (I left out the none-important parts like PK and properties):
<class name="Member" table="mem" >
<component name="wallet" class="Wallet">
<set name="transactions" table="wallet_tx" cascade="all">
<cache usage="read-write" />
<key column="idTaxer" not-null="true" />
<composite-element class="WalletTransaction">
<property name="amount" type="big_decimal" column="amount" />
<many-to-one name="product" column="idPrdInst" class="Product" cascade="none" />
</composite-element>
</set>
</component>
<set name="products" cascade="delete" inverse="true">
<key column="idTaxer" not-null="true" />
<one-to-many class="Product" />
</set>
</class>
<class name="Product" table="prd" >
...
<many-to-one name="member" column="idMember" class="Member" cascade="none" />
</class>
The DB error:
ERROR: update or delete on table "prd" violates foreign key constraint "fk_1umej7" on table "wallet_tx"
DETAIL: Key (id)=(75bef42fc4544) is still referenced from table "wallet_tx".