Playing with this some more, I don't even think the setX() approach will
always work.

Given the following:

Customer <--------- Address

ShippingAddress extends Address
BillingAddress extends Address


The following code does not really yield expected results either:

assertEquals(0, c.getShippingAddresses());
ShippingAddress sa;
sa.setCustomer(c); 
assertEquals(1, c.getShippingAddresses());

The last assertion will fail in the current context.  My guess is that
Cayenne is using Address's setCustomer() rather than ShippingAddress's or
that it is having difficulty resolving the type.  A new context reading the
value from the DB will return the entry in getShippingAddresses() just fine.
Likewise, using the other end of the relationship yields proper results
(i.e., calling c.addToShippingAddresses(sa)).

I guess a manual duplicate check is the only reasonable approach in that
case.

-- 
Kevin

On 3/5/08 12:11 PM, "Mike Kienenberger" <[EMAIL PROTECTED]> wrote:

> For what it's worth, I have always avoided this issue by only using
> the artist.setPainting(painting) method and never the
> painting.addToArtist(artist) method.
> 
> Another possibility would be to have the generated template addTo*
> method do the check as Andrus points out:
> 
> if(!user.getRoles().contains(role)) {
>      user.addToRoles(role);
> }

Reply via email to