[Opensource] cloning auto-inc dbobjects

larry hamel expresso at codeguild.com
Tue Sep 30 15:35:14 PDT 2003


here's a "hacky" technique for deep cloning of dbobjects which have auto-inc keys.  this could be dangerous if original could not be restored... but we throw an exception in that case anyway... is there a better way that is this that is just as easy?  seems like DBObject could support cloning easily, even in the general case w/o auto-inc.

larry
-------------------

    /**
     * deep copy fields to new object
     */
    public Attribute clone(String cloneNodeId) throws DBException {
        String origId = getId();
        String cloneId = null;

        try {
            this.setId("0");  // resetting id isn't technically necessary, since auto-inc field WILL get new value with add() method
            // clone by just adding
            this.add();
            cloneId = this.getId();
        } finally {
            // make sure we reset even if there are problems
            this.setId(origId);
            this.retrieve();
        }

        Attribute clone = new Attribute(getRequestingUid());
        clone.setDBName(getDBName());
        clone.setAttributeId(cloneId);
        clone.retrieve();

        return clone;
    }




More information about the Opensource mailing list