java - Hibernate performs delete cascade in wrong order -


my db throwing constraing violation (fk) because hibernate performs cascade delete in wrong order.

details: delete member, has wallet wallet transactions (value-type), , wallet transaction has association product member contains collection of products (see hibernate mapping below).

i delete member instance, , want hibernate remove both products , wallet transactions. seems first removes product instances (through cascading), such fk violation thrown db, it's still referenced wallet transaction, wasn't removed yet (through cascade)

i played around cascade setting, all-delete-orphan (on products), etc..., no luck emptied wallet transactions , flushed hibernate session in same removal transaction, same error.

please understanding , order of cascade deletion correct?

the hibernate mapping (i left out none-important parts pk , 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) still referenced table "wallet_tx". 

if empty wallet transaction , expect deleted must set delete-orphan on transactions relationship.

if product still deleted 1st, can flush after removing wallet tx transaction set, work it's surely not state-of-art way.

otherwise try map product - transaction onetomany relationship , set delete cascade on (with inverse), product deletion first trigger transaction deletion.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -