users@glassfish.java.net

JPA: how to alter relation without fetching entire entities?

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Sat, 19 May 2007 18:53:14 +0200

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?