RE: Clone an Object and Create New PK

From: Bret Gregory (bret.gregor..ccesspointinc.com)
Date: Thu Mar 25 2004 - 12:58:33 EST

  • Next message: Mike Kienenberger: "Re: Clone an Object and Create New PK"

    Well, it doesn't look like cloning an object manually will be all that
    difficult.
    I am however running into an issue with the 'writePropertyDirectly' method
    on CayenneDataObject.

    My basic code is below:

        Iterator keys = oldObj.getCurrentSnapshot().keySet().iterator();
        while(keys.hasNext()) {
          String key = (String)keys.next();
          newObj.writePropertyDirectly(key, attr.get(key));
        }

    However the insert statement is all nulls. Unfortunately the writeProperty
    which the auto generated classes use has protected status. Am I
    misunderstanding what the writePropertyDirectly method does? Is there a way
    around this without reflecting over the class to get and execute the methods
    that I want?

    -----Original Message-----
    From: Mike Kienenberger [mailto:mkienen..laska.net]
    Sent: Tuesday, March 23, 2004 4:51 PM
    To: Bret Gregory
    Cc: 'cayenne-use..bjectstyle.org'
    Subject: Re: Clone an Object and Create New PK

    Bret Gregory <bret.gregor..ccesspointinc.com> wrote:
    > I was wondering if there was an easy way to create duplicate objects
    > with their relationships while creating new auto_generated primary
    > keys.
    >
    > I have a table with a 1:n relationship to itself.
    > So an Object can be related to a bunch of other objects.
    >
    > I would like to clone the parent object and all of its ancestors
    > without having to manually copy all of their different attributes and
    relationships.
    > Does anyone know of an easy way to do this?

    I don't know if there's Cayenne-specific support for cloning objects.

    I know that under WebObjects I did this by creating a new data object,
    copying the source data object's attributes to the new data object using
    generic data object methods, then creating Entity-specific code for
    rebuilding relationships.

    I used a Map of Maps to track created objects so I could assign the proper
    relationships, since all of the objects (many different Entity types) had to

    be created before the relationships could be reset.

    I can give you some specific WebObject code examples of this if you end up
    having to go the same route.
    The API for Cayenne should be similar enough for it to be useful.

    Copying objects without relationships is pretty trivial, but once you have
    to deal with relationships, things get complicated very quickly.

    -Mike



    This archive was generated by hypermail 2.0.0 : Thu Mar 25 2004 - 12:59:48 EST