views:

49

answers:

1

I am using a combination of Spring 2.5.6 and Hibernate Annotations. I have three objects(tables or w/e) under consideration: Customer, Address, Order. Customer has the Cascade DELETE_ORPHANS property set for addresses.

What i am doing is a customer merge, i'm moving all addresses and orders from one customer to another, then setting a disabled bit on the old customer. I do this by removing addresses from CustB and adding them to CustA. Then i save CustA with .update(custA), the addresses and orders move appropriately, but when I update custB with the disabled bit it erases my moved addresses!

When i comment out the DELETE_ORPHANS it works fine.

How do I stop DELETE_ORPHANS from removing the moved addresses? Should I not remove them from the custB and just change their customer reference? If i do it all within a transaction insead of detaching the objects inbetween each operation will they update properly with DELETE_ORPHAN enabled?

A: 

I found this post on hibernate's forums: https://forum.hibernate.org/viewtopic.php?f=1&t=961777&start=0

Christian from the hibernate team talks about DELETE_ORPHANS as a hack that ensures hibernate that when the associated is removed from an object it is ok to delete. He says to use this sparingly.

Basically, don't use delete_orphans if you can help it.

If you do decide to use it, you'll have to do extra queries that delete entries then re-create them with new identifiers attached to the new parent objects.

Martin Dale Lyness