| Does deleting a relationsip remove the dependant object from the database? [message #654540] |
Wed, 16 February 2011 04:16  |
Tony Dietrich Messages: 8 Registered: February 2011 Location: UK |
Junior Member |
|
|
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 ?
class Person{
....
@OneToMany(mappedBy="telephone", cascade=CascadeType.ALL, orphanRemoval=true)
private Collection<TelephoneNumber> telephoneNumberCollection;
...
}
*************************
Person p=personFacade.getPersonForID(id);
List<TelephoneNumber> lst=p.getTelephoneNumberCollection();
For(TelephoneNumber t:lst){
if(t.getName().equals("Home") lst.remove(t);
}
personFacade.merge(p);
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 Messages: 862 Registered: July 2009 |
Senior Member |
|
|
Hello Tony,
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.
Best Regards,
Chris
[Updated on: Wed, 16 February 2011 09:03] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02011 seconds