-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate deletes data in the wrong order - causes fk probs
PostPosted: Tue Mar 02, 2010 12:56 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
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


Top
 Profile  
 
 Post subject: Re: Hibernate deletes data in the wrong order - causes fk probs
PostPosted: Tue Mar 02, 2010 10:33 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
What I personally suggest is to use the cascade delete option only for real 'child' objects.
In UML this would be aggregated objects (in contrast to associatated objects).
If you apply cascade delete option on unidirectional ManyToOne associations,
then you easy will run into this problem as soon an entity is associated by more than one entity.
And: Unidirectional associations I always remove manually before deleting the target entity, so I have no fk-problems.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.