RE: Fetching a NextVal to be used in an insert

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Mon Nov 01 2004 - 14:05:26 EST

  • Next message: Williams, Beth: "RE: Fetching a NextVal to be used in an insert"

    I suspect that validation failure happens because "personId" is mapped as
    an object property and Cayenne checks that it is NULL *prior* to PK
    generation.

    So what you can do is remove a personId ObjAttribute from the mapping, and
    if you still need access to it, use DataObjectUtils.pkForObject().

    Andrus

    > Do you do any custom validations on the personId field?
    > validateForInsert(), for example, would run prior to primary key
    > generation, I think. Also, it sounds like you are using Oracle. Do you
    > have a sequence set up for your Persons table?
    >
    > /dev/mrg
    >
    > -----Original Message-----
    > From: Williams, Beth [mailto:beth.william..FIB.ORG]
    > Sent: Monday, November 01, 2004 1:37 PM
    > To: cayenne-use..bjectstyle.org
    > Subject: RE: Fetching a NextVal to be used in an insert
    >
    >
    > Thanks Brian. That was how I understood it to work, however, that was
    > not what I experienced. Here is the code I'm using:
    >
    > Persons newPerson = (Persons)
    > cayenneContext.createAndRegisterNewObject(Persons.class);
    > newPerson.setLastName("Williams");
    > newPerson.setFirstName("Beth");
    >
    > cayenneContext.commitChanges();
    >
    > The personId field is set as the primary key, however I get a message
    > saying that the field cannot be null when I try and run the code. Do I
    > have to do something in the code to tell it to try and set the personId?
    > Unfortunately, the database I am working with will be unavailable the
    > rest of the day so I cannot run the code and give you the actual error
    > messages I'm receiving. Thanks.
    >
    > Beth
    >
    > -----Original Message-----
    > From: Bryan Lewis [mailto:brya..aine.rr.com]
    > Sent: Monday, November 01, 2004 12:00 PM
    > To: cayenne-use..bjectstyle.org
    > Subject: Re: Fetching a NextVal to be used in an insert
    >
    >
    > Williams, Beth wrote:
    >> I am using the "createAndRegisterNewObject()" method from the
    > DataContext to essentially add a new person to our persons objEntity.
    > One of the attributes that is needing to be set is the personId. This
    >>field in the database is sequence number, however, there is no trigger
    > setup to automatically generate this number. What I need to do is find
    > out what the next number in the sequence would be (equivalent to running
    > the following query select person_id.NextVal from dual) so that I can
    > use the setPersonId() method and include this value. Can this be done
    > through Cayenne and if so, what would be the best way of doing this?
    > Thanks.
    >
    > After the createAndRegisterNewObject(), in the next commitChanges(),
    > Cayenne will automatically do the fetching of NextVal to get the next
    > primary key value, assuming that personId is designated as a primary
    > key. (You can see the NextVal fetch in the log.) You shouldn't have to
    >
    > worry about it in your code.
    >
    > Once in a blue moon, you might want to supply a different value for the
    > primary key, say by letting the user specify it. This is possible with
    > some extra code, although this probably isn't what you were asking.



    This archive was generated by hypermail 2.0.0 : Mon Nov 01 2004 - 14:05:28 EST