RE: Clone an Object and Create New PK

From: Bret Gregory (bret.gregor..ccesspointinc.com)
Date: Thu Mar 25 2004 - 14:00:07 EST

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

    Well, I think I figured it out. The SnapShot uppercases all the keys,
    however the properties of the object in question are all lowercase, so
    forcing the keys to lower case was the answer for me.

    I am still doing a deep copy of the object, but now it is just a matter of
    looping over the relationships and basically doing the same logic in order
    to copy the data from the old to the new. I basically, just mimic'd the
    writeProperty by forcing the peristance state on the object to new. I am
    not sure why I had to do this since I had just created and registered the
    new object above this block of code. After creating the new object but
    before I commit, I am setting different properties. These seem to be
    ignored, unless the new object's persistance state is changed.

    Thanks for the help Mike, I appreciate it, you definitely gave me some ideas
    on how to do things more efficiently.

    -Bret Gregory

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

    Bret Gregory <bret.gregor..ccesspointinc.com> wrote:
    > Well, it doesn't look like cloning an object manually will be all that
    > difficult.

    Yes, a shallow copy is very easy, as long as you're ok with all
    relationships pointing to the original relationship targets. Your original
    message spoke of cloning the relationships as well.

    > 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.

    It's beyond my knowledge to guess why this would be the case. Maybe you
    could try calling oldObj.resolveFault() before (or possibly after)
    getCurrentSnapshot() and see if that fixes the problem.

    > Unfortunately the writeProperty
    > which the auto generated classes use has protected status.

    It was mentioned on the developer list last week that writeProperty should
    be made more visible.
    That doesn't help you much unless you're using the latest cvs pull (and it
    assumes that the change made it into cvs already.)

    For now you could just duplicate the functionality of writeProperty:

        protected void writeProperty(String propName, Object val) {
            resolveFault();

            // 1. retain object snapshot to allow clean changes tracking
            // 2. change object state
            if (getPersistenceState() == PersistenceState.COMMITTED) {
                setPersistenceState(PersistenceState.MODIFIED);
                getDataContext().getObjectStore().retainSnapshot(this);
            }
            // else....
            // other persistence states can't be changed to MODIFIED

            writePropertyDirectly(propName, val);
        }

    > 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?



    This archive was generated by hypermail 2.0.0 : Thu Mar 25 2004 - 14:01:20 EST