You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Dave Dombrosky <do...@gmail.com> on 2008/08/19 07:45:57 UTC

Should commitChangesToParent() call pre-persist method?

Should commitChangesToParent() call my pre-persist method?

I have coded up a pre-persist listener to set my object's type for
cayenne inheritance, and also to create some new objects to fill in
required relationships.  The problem I am running into is that my
pre-persist method gets called twice.  Once when i register my object
with the child context, and once again when the changes are committed
to parent.  This causes issues because I my relationships point to
unfilled objects, and when I try to commit I get validation errors
because of these extra objects.

Is this the expected behavior of pre-persist?  If so, then where would
be a good place to initialize required relationships?

-Dave

Re: Should commitChangesToParent() call pre-persist method?

Posted by Andrus Adamchik <an...@objectstyle.org>.
commitChangesToParent() calls pre-persist on the parent's copy of the  
object, so technically it is called once per object instance. Now I  
agree that the callback mechanism was designed with no concept of  
nested contexts in mind (we cloned it from the JPA spec)... I guess  
once way to address the problem is to check where it is called, and  
skip it either for the child or the parent context:

void prePersist() {
    if(getObjectContext().getChannel() instanceof ObjectContext) {
         // nested context
         ....
    }
    else {
         // topmost context
         .....
    }
}

Andrus


On Aug 19, 2008, at 8:45 AM, Dave Dombrosky wrote:

> Should commitChangesToParent() call my pre-persist method?
>
> I have coded up a pre-persist listener to set my object's type for
> cayenne inheritance, and also to create some new objects to fill in
> required relationships.  The problem I am running into is that my
> pre-persist method gets called twice.  Once when i register my object
> with the child context, and once again when the changes are committed
> to parent.  This causes issues because I my relationships point to
> unfilled objects, and when I try to commit I get validation errors
> because of these extra objects.
>
> Is this the expected behavior of pre-persist?  If so, then where would
> be a good place to initialize required relationships?
>
> -Dave
>