-->
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.  [ 3 posts ] 
Author Message
 Post subject: Cascade "all-delete-orphan" does not work for list
PostPosted: Thu Jun 18, 2009 10:14 am 
Newbie

Joined: Fri Oct 03, 2008 8:23 am
Posts: 4
Hello everyone,


I have problem in trying to make delete orphans works.
The problem is that,
when i remove a child from a list, and save the parent,
the child is still in the database.

The code is like this :

Code:
 
Parent p = myDao.findParent(id);
p.getChildren().remove(0);
myDao.save(p);



This is the mapping.
Code:
<!-- in the parent class -->
      <list name="children" inverse="false"
            cascade="all,all-delete-orphan"
       >
         <key column="id_actionProg"  ></key>
         <list-index column="sortOrder" />
         <one-to-many class="OperationProgrammee" />
      </list>
   



I already use "all-delete-orphan" cascade style as suggested in the Hibernate documentation. "21. Example: Parent/Child".
http://docs.jboss.org/hibernate/stable/core/reference/en/html/example-parentchild-cascades.html.
This does not work.
Maybe this is because i use "inverse=false" or the collection is a list?

Please help me.
Thank you in advance.

regards,


Top
 Profile  
 
 Post subject: Re: Cascade "all-delete-orphan" does not work for list
PostPosted: Thu Jun 18, 2009 11:36 am 
Newbie

Joined: Thu Jun 18, 2009 11:23 am
Posts: 2
I think the reason could be that you are not updating the relation from Child's end. For e.g. you have done:
Code:
Parent p = myDao.findParent(id);
p.getChildren().remove(0);
myDao.save(p);


However you also need to update the Child's reference with some code like:

Code:
Parent p = myDao.findParent(id);
Child c = p.getChildren().get(0);
p.getChildren().remove(0);
c.setParent(null);
myDao.save(p);


Try this out. I hope it works.


Top
 Profile  
 
 Post subject: Re: Cascade "all-delete-orphan" does not work for list
PostPosted: Thu Jul 09, 2009 9:39 am 
Newbie

Joined: Fri Oct 03, 2008 8:23 am
Posts: 4
Thanks riks.java for the response.


However, I still have the question.

In my case, the association is one way (I use "inverse=false").
That is, only the parent references a list of children, but the children do not have a backward reference to the parent.
I find this approach more programmer-friendly : I do not need to concern about the consistency between two ends (parent -> children, children -> parent).


However, I see that this approach prevents Hibernate to correctly remove orphan children when I update the parent.

Anyone knows a solution that does not require backward references please ?


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