Re: Problem with PK

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Sun Jan 15 2006 - 11:21:07 EST

  • Next message: Andrus Adamchik: "Re: Problem with PK"

    Do not map "id" as as an ObjAttribute in the Modeler - this should
    address the first problem. If you need access to id, you can create a
    getId() method in ExtResources class that relies on DataObjectUtils
    (see below).

    Regarding the second problem. By default id is not known until object
    is committed. But you can generate it yourself and stick in the
    ObjectId. You can implement "getId()" method mentioned above like
    that (assuming Cayenne 1.2) -

    public int getId() {
        ObjectId oid = getObjectId();

        if(oid.isTemporary() && !id.isReplacementIdAttached()) {
            // generate id here
            updateId();
        }

        // now we can safely use DataObjectUtils
        return DataObjectUtils.intPKForObject(this);
    }

    void updateId() {
        DbEntity entity = getDataContext().getEntityResolver
    ().lookupDbEntity(this);
        DataNode node = getDataContext().lookupDataNode(entity.getDataMap
    ());

        Object pk = node.getAdapter().getPkGenerator
    ().generatePkForDbEntity(node, entity);
        getObjectId().getReplacementIdMap().put
    ("<actual_pk_attribute_name>", pk);
    }

    Andrus

    On Jan 15, 2006, at 11:05 AM, Chronos wrote:

    > Hey!
    >
    > I´ve got two little problems, I hope somebody can help me to solve it:
    >
    > 1. I will generate a new entry:
    >
    > ExtResources newRes = (ExtResources)
    > ctxt.createAndRegisterNewObject(ExtResources.class);
    > newRes.setContent(content);
    > newRes.setContent(content);
    > ...
    > ctxt.commitChanges();
    >
    >
    > But then I get an exception:
    > "Validation failure for
    > edu.unika.aifb.webex.webexauthor.server.model.ExtResources.id:
    > "id" is
    > required",
    > but ID is my PK, so how can I add this entry?
    >
    > 2. When I add this entry, is there a possibility to get the ID or
    > must I
    > use DataObjectUtils.objectForPK etc.?
    >
    > Thans for your help,
    >
    > gr
    >
    > PK
    >



    This archive was generated by hypermail 2.0.0 : Sun Jan 15 2006 - 11:21:09 EST