persistence@glassfish.java.net

Re: [Fwd: Cascade persist and 1ToMany relationship?]

From: Tom Ware <tom.ware_at_oracle.com>
Date: Tue, 29 Nov 2005 10:11:54 -0500

Marina,

  It is my understanding that regardless of the cascade setting, you
have to set both relationships. The EJB 2.1 specification allowed only
one side of a relationship to be set, but as I understand it, that has
been removed.

  I believe the cascade functionality is not provided as a way to set up
your relationships automatically. It just lets you specify the
granularity level with which you have to persist.

-Tom

Marina Vatkina wrote:

>Team,
>
>I've sent this question to persistence alias on Tuesday,
>but it never went through. So I'm resending it to both
>aliases as I still don't have luck with persistence alias.
>
>Note: Our current implementation cascades with
>a1.getBs().add(b1) but this doesn't eliminate
>the original concern that ownership rules seem
>to contradict the cascade rules.
>
>thanks,
>-marina
>
>-------- Original Message --------
>Subject: Cascade persist and 1ToMany relationship?
>Date: Tue, 22 Nov 2005 10:25:02 -0800
>From: Marina Vatkina <marina.vatkina_at_sun.COM>
>To: "persistence_at_glassfish.dev.java.net" <persistence_at_glassfish.dev.java.net>
>
>Team,
>
>I'm a bit confused about the rules for creating a set of related
>instances in the same tx and persisting them using CASCADE=ALL
>setting. The relationships are bidirectional and FK-based.
>
>Let's say I have A with a collection of 'bs', and B with a property 'a'.
>
>A a1 = new A(...);
>B b1 = new B(...);
>B b2 = new B(...);
>
>>From which side do I need to set relationships to call em.persist(a1)
>and see the magic?
>
>If I do it from B (b1.setA(a1)), a1 doesn't know about the related instance
>to persist it.
>
>If I do it from A (a1.getBs().add(b1)), then according to the spec, the
>relationship
>change won't be propagated because the Many side owns the relationship.
>
>If I set it from all sides, what do I gain as I could just persist all of them
>separately - it's the same ammount of code?
>
>thanks,
>-marina
>
>

-- 
Tom Ware
Principal Software Engineer
Oracle Canada Inc.
Direct: (613) 783-4598
Email: tom.ware_at_oracle.com