Hi, I have a database that is somewhat weird in that it eventually comes full circle. The domain model really needs to if I want to model it accurately. No way around it.
For example, one arm of the entity graph goes like this:
M->AM->AT->AQ->R->K
Another arm of the of the graph starting from entity 'M' goes like this:
M->T->Q->K
As you can see, both arms eventually point to K. What I want to do is delete an instance of M, and cascade delete everything on both arms. However, I want to delete all the instances in "M->AM->AT->AQ->R" part first, then go back and delete "M->T->Q->K". This would satisfy the foreign key constraints.
However, Hibernate favours going the opposite way... so when it finally arrives at K and tries to delete a K that is being used by an R, it bombs due to fk constraint.
I would appreciate help. I can only think of a few ideas:
1. Split K into two entities K1 and K2 to kill the eventual circular dependency. This will mess up many aspects of the system which are working just fine and as intended.
2. Use JDBC to delete the records manually, which is not something I really want to do if I can help it. There would be too many instances where this code needs to be run... such as deleting "T", "Q" and many other entities not listed.
3. Turn off foreign key constraints altogether. Wise?
Is there a simpler solution? Thanks
|