-->
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.  [ 1 post ] 
Author Message
 Post subject: Delete parent > set child's ref column to null
PostPosted: Tue Oct 23, 2007 4:14 pm 
Newbie

Joined: Mon Sep 17, 2007 1:28 pm
Posts: 6
Hi,

I can't figure this out and I hope someone out there has.

Code:
class Parent  {

     @OneToMany(mappedBy="parent")
     private List<Child> children;

     void addChild(Child c)  {
          c.setParent(this);
          getChildren().add(c);
     }
}


class Child  {
    @ManyToOne
    private Parent parent;
}


{
    Parent p = findById(4);
    p.addChild(childService.findById(7));
    p.addChild(childService.findById(8));
    update(p);

    delete(p);   
}


If I have no cascade set on the parent's annotation, I get a FK error. If I set cascade delete, it deletes the parents children - which I do not want.

I want the children to remain in the db for later. So, the call to delete should null out the parent column on each child. Obviously I could subclass my hibernate thing and put a for loop in there to do that, but there's got to be a way to

@OnDelete(action=OnDelete.SET_NULL) ?

instead of

Code:
class ParentService extends Service<Parent>  {
    @Override
    void delete(Parent p)  {
       for (Child c : p.getChildren)  {
           c.setParent(null);
           childService.update(child);
       }
    }


Any ideas? TIA


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

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.