Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » OneToMany associations
OneToMany associations [message #385412] Fri, 30 January 2009 00:59 Go to next message
David J Rericha is currently offline David J RerichaFriend
Messages: 13
Registered: July 2009
Junior Member
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] Fri, 30 January 2009 04:35 Go to previous message
Doug Clarke is currently offline Doug ClarkeFriend
Messages: 155
Registered: July 2009
Senior Member
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: Fri Apr 26 12:18:49 GMT 2024

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

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

Back to the top