users@glassfish.java.net

Re: JPA: how to alter relation without fetching entire entities?

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Mon, 21 May 2007 12:50:19 -0700

Hi Witold,

You can call addressToRemove.getPersons().clear() to clear the other side. There
might be persistence providers who can help you with changing relationships
without fetching all related instances, but it's beyond the spec. If you use
TopLink Essentials, it's very important to always maintain all the relationships
from all sides because the 2-nd level cache stores it in the same way as the EM
knew it at the time of commit, and it's being copied over to the new EM (PCtx)
as-is (i.e. no relationship management performed for you by the provider).

regards,
-marina

Witold Szczerba wrote:
> Hi there,
> is there any way to alter some relation without fetching entire entities?
> For example, lets consider this:
> person contains address:
> class Person:
> ....
> @OneToMany
> Set<Address> addresses;
> ....
>
> Now, if I know certain address to remove from person, I have to:
> 1) fetch person:
> Person p = em.find(Person.class, personId);
>
> 2) fetch collection of addresses that person contains:
> Set<Address> addresses = p.getAddresses();
>
> 3) get address reference:
> Address addressToRemove = em.getReference(Address.class, addressId);
>
> 4) remove addressToRemove from collection:
> addresses.remove(addressToRemove);
>
> If Address would contain Set<Person> persons relation field, then one
> would have to fetch address as well and remove "p" from that
> collection...
>
> As one can see, step 1,2 and 3 requires massive database
> communication, HUGE amount of data does need to be fetched only to
> accomplish VERY simple task, in SQL it would bee something like:
> DELETE FROM PersonAddresses WHERE person_id = ? and address_id = ?
>
> Is there such an equivalent in JPA?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>