Re: how to specify objectId for new object

From: Bryan Lewis (brya..aine.rr.com)
Date: Mon May 03 2004 - 20:20:31 EDT

  • Next message: David Solis: "RE: Tapestry framework design applied on Swing app"

    Eric Schneider <eric <at> centralparksoftware.com> writes:

    > Maybe you can post what you have? There maybe some shortcuts that will
    > help you slash away some of that code.

    Okay...

    // This is one of the few objects where we want to allow the user to
    // specify the 'oid' primary key; the oid attribute is exposed in the
    // entity. When the user _doesn't_ specify an oid, we have to supply one
    // by getting the next value ourselves.
    public Integer getNewOid()
    {
        DataContext dc = getDataContext();

        // Doing it portably without SQL.
        // Get the DataMap; we have only one in our model.
        Collection dataMaps = dc.getDataMaps();
        Iterator it = dataMaps.iterator();
        DataMap dataMap = (DataMap) it.next();

        DataNode dataNode = dc.lookupDataNode(dataMap);
        EntityResolver resolver = dataNode.getEntityResolver();
        DbEntity dbEntity = resolver.lookupDbEntity(this);
        PkGenerator pkGenerator = dataNode.getAdapter().getPkGenerator();

        Integer oid = null;
        try {
            Object pk = pkGenerator.generatePkForDbEntity(dataNode, dbEntity);
            oid = (Integer) pk;
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }

        return oid;
    }



    This archive was generated by hypermail 2.0.0 : Mon May 03 2004 - 20:20:41 EDT