Re: nested dataContexts

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Thu Jan 11 2007 - 13:50:01 EST

  • Next message: Steve Wells: "Re: Cayenne Annotations"

    Hi Edward,

    Sorry for delayed reply.

    >> AnotherClass newObject =
    >> (AnotherClass)childContext.createAndRegisterNewObject
    >> (AnotherClass.class);
    >> AClass localParentObject = (AClass)childContext.localObject
    >> (parent.getObjectId(),null);
    >>
    >> object.setParent(localParent);

    I assume 'object' is same as 'newObject'?

    >> For some reason I seem to get the parent having two children, not
    >> just the
    >> one! I assume it is because the changes in the childContext are
    >> propagated
    >> to the parent context so as a result the first setParent call adds
    >> the child
    >> to the parent and on commit changes the child is added again to
    >> the parent.

    No - actually the changes are only propagated on commit. So this
    behavior does look like a bug. I suggest to check whether this
    happens with the latest code (unofficial 2.0.2 and 1.2.2 builds can
    be downloaded from here: http://people.apache.org/~aadamchik/release/
    2.0.2/ ) If it still happens, please open a bug report.

    > What is the real purpose of passing a prototype?

    To merge data from a different object. I must admit that cramming all
    possible merge scenarios in a single 'localObject' method does create
    some confusion. We may come up with a better way to transfer and
    merge objects in the future.

    Andrus

    On Jan 8, 2007, at 11:48 AM, edward pedersson wrote:
    > Have been digging around the dataContext and I saw this in the
    > localObject
    > method
    >
    > // TODO: Andrus, 1/24/2006 implement smart merge for
    > modified
    > objects...
    > if (cachedObject != prototype
    > && state != PersistenceState.MODIFIED
    > && state != PersistenceState.DELETED) {
    >
    > I believe this may be causing one of the problems below. If you call
    > localObject with a new object and a prototype then the checks above
    > are true
    > and as a result this gets called further down the chain in the same
    > method
    >
    > descriptor.shallowMerge(prototype, cachedObject);
    >
    > which sets all the relationships to faults and this breaks because
    > the new
    > object is not committed.
    >
    > Is this a bug? What is the real purpose of passing a prototype?
    >
    > On 05/01/07, edward pedersson <cpsmadnes..ooglemail.com> wrote:
    >>
    >> Hi
    >>
    >> I am using the latest version of Cayenne with Tapestry 4.0.1.
    >>
    >> I am having a bit of trouble using nested dataContexts. I would
    >> like the
    >> user to be able to create a new object in a wizrd style
    >> application but have
    >> the option to cancel half way through the stages.
    >>
    >> I have tried a few different options and noe seem to work. In the
    >> first
    >> instance I tried the nested datacontext like this:
    >>
    >> AClass parent; //assume this exists and is in the threaded context
    >>
    >> DataContext childContext = this.getDataContext().createChildContext
    >> () //
    >> this.getDataContext() is the threaded context
    >>
    >> AnotherClass newObject =
    >> (AnotherClass)childContext.createAndRegisterNewObject
    >> (AnotherClass.class);
    >> AClass localParentObject = (AClass)childContext.localObject(
    >> parent.getObjectId(),null);
    >>
    >> object.setParent(localParent);
    >>
    >> //do other stuff here like set other properties
    >>
    >> // now save to parent
    >>
    >> childContext.commitChangesToParent();
    >>
    >> For some reason I seem to get the parent having two children, not
    >> just the
    >> one! I assume it is because the changes in the childContext are
    >> propagated
    >> to the parent context so as a result the first setParent call adds
    >> the child
    >> to the parent and on commit changes the child is added again to
    >> the parent.
    >>
    >> So I tried to have separate dataContexts instead by doing this
    >>
    >> DataContext anotherContext = DataContext.createDataContext()
    >>
    >> AnotherClass newObject =
    >> (AnotherClass)anotherContext.createAndRegisterNewObject
    >> (AnotherClass.class
    >> );
    >> AClass localParentObject = (AClass)anotherContext.localObject(
    >> parent.getObjectId(),null);
    >>
    >> object.setParent(localParent);
    >>
    >> //do other stuff here like set other properties
    >>
    >> // now save to parent
    >> newObject = (AClass)this.getDataContext().localObject(
    >> newObject.getObjectId(),null);
    >>
    >>
    >> I get errors like Can't build a query for relationship '...' for
    >> temporary
    >> id:
    >>
    >> Does anyone knew what I am doing wrong?
    >>
    >> many thanks for your help.
    >>
    >> --
    >>
    >>
    >> -- e
    >
    >
    >
    >
    > --
    >
    >
    > -- e



    This archive was generated by hypermail 2.0.0 : Thu Jan 11 2007 - 13:50:44 EST