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.  [ 4 posts ] 
Author Message
 Post subject: One-to-Many association delete
PostPosted: Mon Aug 24, 2009 9:03 pm 
Beginner
Beginner

Joined: Sat Dec 03, 2005 11:12 am
Posts: 27
Hi,
I have a Parent child relationship between two entities. I have cascade = all JPA property between these two entities.

I am doing parent.getChildren().add(child) to add children to the parent class and saving the parent entity which works great.

But, when i do parent.getChildren().remove(child) and save the parent the child record is not removed. What should i do to remove the child?

Thanks,


Top
 Profile  
 
 Post subject: Re: One-to-Many association delete
PostPosted: Mon Aug 24, 2009 9:26 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
What about session.delete(child);

Perhaps this code is just deleting the association to the entity, but not the actual entity itself.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: One-to-Many association delete
PostPosted: Mon Aug 24, 2009 10:10 pm 
Beginner
Beginner

Joined: Sat Dec 03, 2005 11:12 am
Posts: 27
[quote="Cameron McKenzie"]What about session.delete(child);

Perhaps this code is just deleting the association to the entity, but not the actual entity itself.[/quote]

Is this the only way if you want to delete a child entity. I am using JPA and want to know why parent.getChildren().remove(child); update(parent); doesn't work?

Thanks,


Top
 Profile  
 
 Post subject: Re: One-to-Many association delete
PostPosted: Mon Aug 24, 2009 11:35 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
But, when i do parent.getChildren().remove(child) and save the parent the child record is not removed.


Well, I'm just thinking about it logically. If you remove the child from the parent, the association is gone, but does that imply the child should be deleted? It's like divorcing your wife. You can kill the relationship, but that doesn't kill her. Ok, bad analogy, but you know what I mean.

I'm wondering if you checked the parent if you would see the association removed, that is, the foreign key nulled.

What effect would cascade-delete-orphan have on this effect? Have you given that a shot?
Quote:
You can even use cascade="all" to specify that all operations should be cascaded along the association. The default cascade="none" specifies that no operations are to be cascaded.

A special cascade style, delete-orphan, applies only to one-to-many associations, and indicates that the delete() operation should be applied to any child object that is removed from the association.

Recommendations:

It does not usually make sense to enable cascade on a <many-to-one> or <many-to-many> association. Cascade is often useful for <one-to-one> and <one-to-many> associations.
If the child object's lifespan is bounded by the lifespan of the parent object, make it a life cycle object by specifying cascade="all,delete-orphan".
Otherwise, you might not need cascade at all. But if you think that you will often be working with the parent and children together in the same transaction, and you want to save yourself some typing, consider using cascade="persist,merge,save-update".


http://docs.jboss.org/hibernate/stable/core/reference/en/html/objectstate.html

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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