You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Lon Varscsak <lo...@gmail.com> on 2015/09/01 00:04:51 UTC

onPostAdd and nested object contexts

So I am having trouble because when I call commitChangesToParent on one of
my nested ECs, I'm getting onPostAdd called.  So in my case, I'm using
onPostAdd to "setup" my object by creating relationships and setting
default values.  However, it's getting called twice.  Once when I create
the object in the child context and once on the parent context (during
commit).

Any ideas on how to combat this?

-Lon

Re: onPostAdd and nested object contexts

Posted by Lon Varscsak <lo...@gmail.com>.
This doesn't work, because onPostAdd() is called before the object is
initialized with the child's context.  So all property/relationships are
null.

-Lon

On Tue, Sep 1, 2015 at 1:16 PM, John Huss <jo...@gmail.com> wrote:

> Check for null for each property you are setting inside of onPostAdd(),
> like:
>
> public void onPostAdd() {
>     if (getProperty() == null) {
>         setProperty(defaultValue);
>     }
> }
>
> On Tue, Sep 1, 2015 at 2:59 PM Lon Varscsak <lo...@gmail.com>
> wrote:
>
> > I thought of that and tried it, but it's a new uninitialized object.
> > Basically the code I find in Cayenne is creating a new object in the
> parent
> > ec, then merging in changes.  So when onPostAdd is called the object is
> > brand new (no relationships set).
> >
> > -Lon
> >
> > On Tue, Sep 1, 2015 at 3:40 AM, Andrus Adamchik <an...@objectstyle.org>
> > wrote:
> >
> > > Yeah, I must admit it is a bit messy. We may need to redesign callback
> > > logic in respect to the nested contexts. For now your most obvious
> > solution
> > > is to check for the object state within 'onPostAdd' before changing it
> > > again.
> > >
> > > if(getSomeProperty() == null) {
> > >   // we haven't been called yet, so init the property
> > >   setSomeProperty(..);
> > > }
> > >
> > > Andrus
> > >
> > > > On Sep 1, 2015, at 1:04 AM, Lon Varscsak <lo...@gmail.com>
> > wrote:
> > > >
> > > > So I am having trouble because when I call commitChangesToParent on
> one
> > > of
> > > > my nested ECs, I'm getting onPostAdd called.  So in my case, I'm
> using
> > > > onPostAdd to "setup" my object by creating relationships and setting
> > > > default values.  However, it's getting called twice.  Once when I
> > create
> > > > the object in the child context and once on the parent context
> (during
> > > > commit).
> > > >
> > > > Any ideas on how to combat this?
> > > >
> > > > -Lon
> > >
> > >
> >
>

Re: onPostAdd and nested object contexts

Posted by John Huss <jo...@gmail.com>.
Check for null for each property you are setting inside of onPostAdd(),
like:

public void onPostAdd() {
    if (getProperty() == null) {
        setProperty(defaultValue);
    }
}

On Tue, Sep 1, 2015 at 2:59 PM Lon Varscsak <lo...@gmail.com> wrote:

> I thought of that and tried it, but it's a new uninitialized object.
> Basically the code I find in Cayenne is creating a new object in the parent
> ec, then merging in changes.  So when onPostAdd is called the object is
> brand new (no relationships set).
>
> -Lon
>
> On Tue, Sep 1, 2015 at 3:40 AM, Andrus Adamchik <an...@objectstyle.org>
> wrote:
>
> > Yeah, I must admit it is a bit messy. We may need to redesign callback
> > logic in respect to the nested contexts. For now your most obvious
> solution
> > is to check for the object state within 'onPostAdd' before changing it
> > again.
> >
> > if(getSomeProperty() == null) {
> >   // we haven't been called yet, so init the property
> >   setSomeProperty(..);
> > }
> >
> > Andrus
> >
> > > On Sep 1, 2015, at 1:04 AM, Lon Varscsak <lo...@gmail.com>
> wrote:
> > >
> > > So I am having trouble because when I call commitChangesToParent on one
> > of
> > > my nested ECs, I'm getting onPostAdd called.  So in my case, I'm using
> > > onPostAdd to "setup" my object by creating relationships and setting
> > > default values.  However, it's getting called twice.  Once when I
> create
> > > the object in the child context and once on the parent context (during
> > > commit).
> > >
> > > Any ideas on how to combat this?
> > >
> > > -Lon
> >
> >
>

Re: onPostAdd and nested object contexts

Posted by Lon Varscsak <lo...@gmail.com>.
I thought of that and tried it, but it's a new uninitialized object.
Basically the code I find in Cayenne is creating a new object in the parent
ec, then merging in changes.  So when onPostAdd is called the object is
brand new (no relationships set).

-Lon

On Tue, Sep 1, 2015 at 3:40 AM, Andrus Adamchik <an...@objectstyle.org>
wrote:

> Yeah, I must admit it is a bit messy. We may need to redesign callback
> logic in respect to the nested contexts. For now your most obvious solution
> is to check for the object state within 'onPostAdd' before changing it
> again.
>
> if(getSomeProperty() == null) {
>   // we haven't been called yet, so init the property
>   setSomeProperty(..);
> }
>
> Andrus
>
> > On Sep 1, 2015, at 1:04 AM, Lon Varscsak <lo...@gmail.com> wrote:
> >
> > So I am having trouble because when I call commitChangesToParent on one
> of
> > my nested ECs, I'm getting onPostAdd called.  So in my case, I'm using
> > onPostAdd to "setup" my object by creating relationships and setting
> > default values.  However, it's getting called twice.  Once when I create
> > the object in the child context and once on the parent context (during
> > commit).
> >
> > Any ideas on how to combat this?
> >
> > -Lon
>
>

Re: onPostAdd and nested object contexts

Posted by Andrus Adamchik <an...@objectstyle.org>.
Yeah, I must admit it is a bit messy. We may need to redesign callback logic in respect to the nested contexts. For now your most obvious solution is to check for the object state within 'onPostAdd' before changing it again.

if(getSomeProperty() == null) {
  // we haven't been called yet, so init the property
  setSomeProperty(..);
}

Andrus

> On Sep 1, 2015, at 1:04 AM, Lon Varscsak <lo...@gmail.com> wrote:
> 
> So I am having trouble because when I call commitChangesToParent on one of
> my nested ECs, I'm getting onPostAdd called.  So in my case, I'm using
> onPostAdd to "setup" my object by creating relationships and setting
> default values.  However, it's getting called twice.  Once when I create
> the object in the child context and once on the parent context (during
> commit).
> 
> Any ideas on how to combat this?
> 
> -Lon