Skip to main content



      Home
Home » Eclipse Projects » EclipseLink » Does deleting a relationsip remove the dependant object from the database?
Does deleting a relationsip remove the dependant object from the database? [message #654540] Wed, 16 February 2011 04:16 Go to next message
Eclipse UserFriend
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 #654558 is a reply to message #654540] Wed, 16 February 2011 04:27 Go to previous messageGo to next message
Eclipse UserFriend
You only delete something if you execute the remove method of an entity manager. It may very well be possible that this cascades the delete to other entities (this is what the CascadeType is for).

Your code only removes an object from a collection, not from the database.

Tom


On 16-2-2011 10:16, Tony Dietrich wrote:
> 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 #654561 is a reply to message #654558] Wed, 16 February 2011 05:20 Go to previous messageGo to next message
Eclipse UserFriend
Would it have any effect if I included

Person p=personFacade.getPersonForID(id);
 List<TelephoneNumber> lst=p.getTelephoneNumberCollection();
 For(TelephoneNumber t:lst){
 if(t.getName().equals("Home") lst.remove(t);
 }

p.setTelephoneNumberCollection(lst);
 personFacade.merge(p);


I'm looking at
http://en.wikibooks.org/wiki/Java_Persistence/Relationships# Orphan_Removal_.28JPA_2.0.29

which implies that this deletion is specified by JPA 2.0 in circumstances where the dependant object is no longer referenced by the parent object.
Would de-referencing require that the reference is removed on both ends of the relationship though?

If I adopt the alternative approach (specifically deleting the dependant object using EntityManager.remove), is the parent collection automatically updated or do I have to force a refresh?

[Updated on: Wed, 16 February 2011 05:23] by Moderator

Re: Does deleting a relationsip remove the dependant object from the database? [message #654615 is a reply to message #654561] Wed, 16 February 2011 08:48 Go to previous messageGo to next message
Eclipse UserFriend
Nope. Something like lEntityManager.remove(t); would help.


On 16-2-2011 11:20, Tony Dietrich wrote:
> Would it have any effect if I included
>
>
> Person p=personFacade.getPersonForID(id);
> List<TelephoneNumber> lst=p.getTelephoneNumberCollection();
> For(TelephoneNumber t:lst){
> if(t.getName().equals("Home") lst.remove(t);
> }
>
> p.setTelephoneNumberCollection(lst);
>
> personFacade.merge(p);
>
>
> I'm looking at http://en.wikibooks.org/wiki/Java_Persistence/Relationships# Orphan_Removal_.28JPA_2.0.29
>
> which implies that this deletion is specified by JPA 2.0 in circumstances where the dependant object is no longer referenced by the parent object.
> Would de-referencing require that the reference is removed on both ends of the relationship though?
>
>
Re: Does deleting a relationsip remove the dependant object from the database? [message #654616 is a reply to message #654561] Wed, 16 February 2011 08:49 Go to previous messageGo to next message
Eclipse UserFriend
Or, if you do not intend to delete it but simple break the relationship, setting the owning side of the relation. Probably something like lst.setPerson(null);


On 16-2-2011 11:20, Tony Dietrich wrote:
> Would it have any effect if I included
>
>
> Person p=personFacade.getPersonForID(id);
> List<TelephoneNumber> lst=p.getTelephoneNumberCollection();
> For(TelephoneNumber t:lst){
> if(t.getName().equals("Home") lst.remove(t);
> }
>
> p.setTelephoneNumberCollection(lst);
>
> personFacade.merge(p);
>
>
> I'm looking at http://en.wikibooks.org/wiki/Java_Persistence/Relationships# Orphan_Removal_.28JPA_2.0.29
>
> which implies that this deletion is specified by JPA 2.0 in circumstances where the dependant object is no longer referenced by the parent object.
> Would de-referencing require that the reference is removed on both ends of the relationship though?
>
>
Re: Does deleting a relationsip remove the dependant object from the database? [message #654617 is a reply to message #654616] Wed, 16 February 2011 08:56 Go to previous messageGo to next message
Eclipse UserFriend
Thanks for your input Tom Smile
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 Go to previous messageGo to next message
Eclipse UserFriend
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] by Moderator

Re: Does deleting a relationship remove the dependant object from the database? [message #654637 is a reply to message #654619] Wed, 16 February 2011 09:36 Go to previous messageGo to next message
Eclipse UserFriend
I hope so.

Running a test now, I'll let you know if it works.
Re: Does deleting a relationsip remove the dependant object from the database? [message #654669 is a reply to message #654617] Wed, 16 February 2011 10:57 Go to previous message
Eclipse UserFriend
On 16-2-2011 14:56, Tony Dietrich wrote:
> Thanks for your input Tom :)

You're welcome. Just trying to give something back for such a great free(!) software library.
Previous Topic:EclipseLink + Spring can't find Postgres Driver
Next Topic:Error in implementation(?): Calling Stored Procedures in FireBird 2.1
Goto Forum:
  


Current Time: Tue Jul 22 19:17:17 EDT 2025

Powered by FUDForum. Page generated in 0.21038 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top