|Does deleting a relationsip remove the dependant object from the database? [message #654540]
||Wed, 16 February 2011 04:16
| Tony Dietrich
Registered: February 2011
If I have the classic Person entity object, with associated TelephoneNumber objects in a OneToMany relationship, does loading the relationship collection of the parent Person object, then deleting one of the TelephoneNumber objects from this collection and merging the parent object back to the database trigger the deletion of that TelephoneNumber from the database, assuming I have orphanRemoval=true ?|
@OneToMany(mappedBy="telephone", cascade=CascadeType.ALL, orphanRemoval=true)
private Collection<TelephoneNumber> telephoneNumberCollection;
Will this pseudocode result in the removal from the database of the 'Home' telephone number?
|Re: Does deleting a relationsip remove the dependant object from the database? [message #654619 is a reply to message #654561]
||Wed, 16 February 2011 09:00
| Chris Delahunt
Registered: July 2009
Yes, OrphanRemoval will result in PhoneNumbers being deleted from the database when they are no longer referenced from Person. The code in the first post should work, and there is no need to call p.setTelephoneNumberCollection(lst) as you are manipulating the collection already. EntityManager.remove(t) should not be needed - be sure that nothing else references t though, and especially not with a cascade persist/merge relationship, as this will leave your cache inconsistent with the database and potentially cause the object to be resurected later on.
What Tom mentioned is correct when OrphanRemoval or PrivateOwnership (native EclpseLink) is not used.
[Updated on: Wed, 16 February 2011 09:03]
Report message to a moderator
Powered by FUDForum
. Page generated in 0.02648 seconds