-->
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.  [ 5 posts ] 
Author Message
 Post subject: Cascading delete with Foreign Key rel. among cascading items
PostPosted: Thu May 18, 2006 2:53 pm 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
We have a one-many relationship where the "many" items are dependent on each other, and we are using cascading delete to remove the "many" when the "one" is deleted.

E.g. We have a Shape object with a set of Points. One point is the "referencePoint" for another point. If we delete the shape we want to cascade the delete to the points, but if we delete the reference point first, we get a constraint violation.

The effect of all this is a constraint violation depending on the order of the deletes.

What are good ways to work around this? I'm thinking of using the interceptors somehow, or manually cascading this one item. I'm hoping people know best practices about how to do this.


Hibernate version:
3.1.2


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 3:14 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
if you have bi-directional relation with inverse="true" and cascade="all-delete-orphan", the way to delete either shape or point is as follows.


Code:
Shape shape = session.get( Shape.class, shapeId );
// if you want to delete few points of a shape

Point myDeletePoint = getPoint();

if ( shape.getPointsCollection().contains( myDeletePoint ) ) {
    shape.getPointsCollection().remove( myDeletePoint );
    myDeletePoint.setShape( null );
}

// this will delete only point and removes the reference from shape collection
session.saveOrUpdate( shape );

//--------------------
// if you want to delete Shape itself
// when cascade is enabled, all points will also be deleted
Shape shape = session.get( Shape.class, shapeId );
session.delete( shape );


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 3:48 pm 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
Thanks for the reply. My problem is different, however. When the parent is being deleted hibernate removes the children in the wrong order so the constraints between two children are violated.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 4:04 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
I just overlooked this point

One point is the "referencePoint" for another point

if you dont mind, could you explain more?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 4:47 pm 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
Essentially we are using cascading delete to delete a bunch of "children" along with their "parents."

But the children have foreign key constraints among them (both within one parent and between children of different parents), and Hibernate is not ordering the deletes properly even when all related children are being deleted in one transaction.

So I'm leaning towards turning off cascading delete and implementing my own delete algorithm, but don't know how to do this. I'm looking into interceptors, but am open to any solution that will work.

A magic fix of some kind would be ideal, of course.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.