Re: how to specify objectId for new object

From: Bryan Lewis (brya..aine.rr.com)
Date: Mon May 03 2004 - 11:46:44 EDT

  • Next message: Andrus Adamchik: "Re: Generic Relationship Table"

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

    > > But what if we (rarely) want to let the user specify the new object's id?
    >
    > You can do this by adding the pk columns as object attributes in your
    > entity. The modeler leaves these pk columns out of the object entity by
    > default. But, there is nothing that will stop you from adding them
    > yourself. Setup this way, Cayenne will assume that the user will provide
    > the value. Just set the property like you'd set any other.

    Thanks! Works fine.

    Out of curiosity... if I do that and I occasionally want to go back to the auto-
    generated value, what's the best way to get an auto-generated pk in my code?
    I've played with a few approaches, mostly to learn more about Cayenne.

    1. Doing a direct SQL query to the Oracle sequence -- a SqlSelectQuery
    with "select pk_employee.nextval from dual". This works fine but is non-
    portable of course.

    2. The SqlSelectQuery has been deprecated in favor of the new SQLTemplate, but
    I couldn't make the template approach work for this. It expects to return
    DataObjects or else return nothing. Given the same SQL string it returned a
    hollow DataObject with no pk. If the SqlSelectQuery goes away, how should we do
    queries that don't return data objects?

    3. I managed to get an auto-generated pk portably with a series of calls to
    DataMap, DataNode, EntityResolver, DbEntity and PkGenerator. It worked but it
    seemed overly complicated; I was probably doing it the hard way. What's the
    usual practice?

    Thanks again.
    Bryan



    This archive was generated by hypermail 2.0.0 : Mon May 03 2004 - 11:46:56 EDT