Re: Get PK value of a previously generated object

From: Mike Kienenberger (mkienen..mail.com)
Date: Mon Aug 09 2010 - 16:18:10 UTC

  • Next message: mr.abanjo: "Re: Get PK value of a previously generated object"

    It's actually much easier than that.

    // Create main object
    // Create child object

    mainObj.setChild(childObj);

    This will automatically set the correct PK and FK values.

    You can also commit both changes at the same time rather than separately.

    On Mon, Aug 9, 2010 at 12:11 PM, mr.abanjo <mr.abanj..mail.com> wrote:
    > Hi,
    > my application need to generate a "parent" object and then create a "child"
    > object that contains the PK value of the parent.
    > I'm using MySQL and the colum "id" is "autoincrement". ( both tables )
    >
    > So first i create the main object (it extends DataObject ):
    >
    > MainObject mainObj = new MainObject();
    > //setting properties... without id.. it's generated by mysql
    > mainObj.set......
    > ....
    > ...
    > DataContext...registerNewObject(MainObject);
    > DataContext...commitChanges();
    >
    > The record is succesfully inserted in the database!
    >
    > Then i need to get the PK... in this case a single column ( Long ).
    >
    > If i insepct the corresponding filed it is empty, so i see in the
    > documentation that there are two ways to perform this task:
    > http://cayenne.apache.org/doc30/accessing-pk-and-fk-values.html
    > public Long getMainObjectId() {
    > return (getObjectId() != null && !getObjectId().isTemporary()) ? (Long)
    > getObjectId().getIdSnapshot().get(ID) : null;
    > }
    >
    > http://cayenne.apache.org/doc30/dataobjectutils.html
    > long myId = (Long)DataObjectUtils.pkForObject(MainObject);
    >
    >
    > but, with both techniques the value returned is "null"!
    >
    > What is wrong?
    >
    > Thansk for support.
    >
    > Davide
    >



    This archive was generated by hypermail 2.0.0 : Mon Aug 09 2010 - 16:19:05 UTC