My Db throws a constraint violation (FK) because Hibernate cascades in the wrong order.
More: I delete a Member that has a wallet with wallet transactions (value-type), and the wallet transaction has an association with the product, just like a member contains a collection of products (see below for a description of Hibernate).
I am deleting the Member instance and want Hibernate to delete both Wallet products and transactions. It looks like it first deletes the product instances (via cascading), so the BC violation is caused by the database, because it still refers to the Wallet transaction that has not yet been deleted (via the cascade)
I played with setting up the cascade, like all-delete-orphan (for products), etc., but no luck. I also emptied the wallet transactions and dropped the hibernation session into the same delete transaction, but also the same error.
Please understand and help to organize the order of cascading deletion correctly?
Hibernation display (I did not take into account the non-important parts, such as 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>
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".
source
share