Re: Get PK value of a previously generated object

From: mr.abanjo (mr.abanj..mail.com)
Date: Tue Aug 10 2010 - 08:46:25 UTC

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

    Hi Mike,
    if i understand correctly i must configure in cayenne datamap xml file the
    relation between the two objects, and than declare an attribute in the main
    object that contains the "child".
    I know that this is the best way ( if i want to model a database the
    relations must be declared ) but why with the DataObjectUtils i don't
    receive the value?
    There is something i do in a wrong way?

    Thanks
    Davide

    P.S. I wrote this code in a wrong way in my previous email :

    DataContext...registerNewObject(MainObject);

    Obviously the "MainObject" is the isntance and not the class :-)

    On Mon, Aug 9, 2010 at 6:18 PM, Mike Kienenberger <mkienen..mail.com>wrote:

    > 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 : Tue Aug 10 2010 - 08:47:04 UTC