Re: Clone an Object and Create New PK

From: Mike Kienenberger (mkienen..laska.net)
Date: Thu Mar 25 2004 - 13:51:18 EST

  • Next message: Bret Gregory: "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 - 13:50:43 EST