Re: newbie question - exception thrown getting PK for new object

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Wed Oct 19 2005 - 10:46:16 EDT

  • Next message: Dave Merrin: "Transactions"

    Hi Chad,

    DataObjectUtils.intPKForObject(website) is intended to work on
    committed objects, so naturally it fails when invoked a few lines
    BEFORE commit. Here is a few things to fix this problem and generally
    straighten PK generation:

    1. "website.setWebSiteId" indicates that you mapped a PK column as a
    meaningful object attribute. I suggest to remove this mapping from
    the model (i.e. remove ObjAttribute that maps to PK DbAttribute). If
    you need access to PK value in runtime, you may optionally create a
    cover getter method (but no setter) that would use
    DataObjectUtils.intPKForObject.

    2. After step 1 is done and classes are regenerated, remove this
    piece of code, as Cayenne will automatically generate PK value on
    commit. No need for any code for this to happen:

    > if (website.getWebSiteId() == null {
    > int id = DataObjectUtils.intPKForObject(website);
    > website.setWebSiteId(new Integer(id));
    > }

    Cayenne will take care of generating a PK.

    3.

    > Note: "PK", "Mandatory" & "Generated"

    "Generated" is used for database autoincrement columns. Only check it
    if (a) your PK is setup to auto-increment value on insert (this is
    not Cayenne related and is generally db specific) and (b) you are
    either on MySQL or SQLServer, as other JDBC drivers do not support
    autoincrement [we will be changing the Modeler screens to make these
    points more explicit]. If this is not the case, uncheck it, but leave
    PK checked - Cayenne will do the right thing.

    Andrus



    This archive was generated by hypermail 2.0.0 : Wed Oct 19 2005 - 10:46:20 EDT