Skip to main content



      Home
Home » Eclipse Projects » EclipseLink » OneToMany associations
OneToMany associations [message #385412] Thu, 29 January 2009 19:59 Go to next message
Eclipse UserFriend
Dear members,

I am experimenting with OneToMany associations in the following senario:

Patient with addresses in a bi-directional one-to-many association (These
are rich patients). I also set cascade=ALL with @PrivateOwned.

I encountered the following:
1) In order to create a patient and add addresses, I must set the patient
in each address and then persist the patient. I cannot just add the
addresses to patient.addresses and save the patient (in this case the
patient and the addresses are saved, but the foreign key is not set in the
addresses). Is this the intended behavior?

2) I am not sure what @PrivateOwned accomplishes. I get the same behavior
with just cascade=ALL when persisting and removing the patient.


@Entity
@Table(name="test_patient")
public class Patient extends Model
{
...
@OneToMany(mappedBy = "patient", cascade = ALL )
@PrivateOwned
private List<Address> addresses = new ArrayList<Address>();...
}

@Entity
public class Address extends Model
{
...
@ManyToOne( fetch=LAZY)
@JoinColumn(name="pat_id", referencedColumnName = "ID")
private Patient patient;...
}

Regards,


David
Re: OneToMany associations [message #385413 is a reply to message #385412] Thu, 29 January 2009 23:35 Go to previous message
Eclipse UserFriend
David,

> 1) In order to create a patient and add addresses, I must set the patient
> in each address and then persist the patient. I cannot just add the
> addresses to patient.addresses and save the patient (in this case the
> patient and the addresses are saved, but the foreign key is not set in the
> addresses). Is this the intended behavior?

Yes, It is up to you to maintain the relationships in the domain model.
This is required of you so that the domain model functions the same when
it is not managed by an entity manager. This is typically done in the
addAddress method:

public void addAddress(Address address) {
this.addresses.add(address);
address.setPatient(this);
}

> 2) I am not sure what @PrivateOwned accomplishes. I get the same behavior
> with just cascade=ALL when persisting and removing the patient.

Yes, cascade-delete will cause the addresses to be deleted when the
Patient is explicitly removed in a transaction. The @PrivateOwned offers
the additional benefit of deleting any address that is removed from the
collection without requiring it to be removed directly.

Doug
Previous Topic:UpdateAllQuery does not seem to execute
Next Topic:Replacing collections
Goto Forum:
  


Current Time: Tue Jul 01 06:16:51 EDT 2025

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

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

Back to the top