Primary Key Always Null

From: Gary Jarrel (garyjarre..rownbilljarrel.com.au)
Date: Thu Nov 27 2003 - 03:20:23 EST

  • Next message: Andrus Adamchik: "Re: Primary Key Always Null"

    Hi Guys!

    I'm reasonably new to Cayenne and currently developing the first project
    using the API.

    My question may seem rather simple but I can't seem to find the correct
    solution.

    I've got 2 tables in the database one is the "Client" the other is
    "Booking". It's a one to many relationship as one client can have many
    bookings. I generated all the classes using the Cayenne Modeller and
    everything seems to be working as normal. The problem that I am having
    as when I want to add a new booking whether by creating a new Client
    object or retrieving an already existing Client from the database. The
    steps that I do are as follows:

    1. Create a new client object and set all the required fields.
    2. Create a new booking object and set all the required fields (except
    for client)
    3. Then I call the "booking.setClient(client)" i.e. adding to the
    booking object the client object
    4. Then I call context.commit()

    However my Junit test case crashes with "client_id" can not be null
    (refering to the "client_id" of the booking record that I'm trying to
    insert into the database). "client_id" is the primary key which Cayenne
    normally generates perfectly fine when I simply add the client object to
    the database using Cayenne API. Even if I use an already existing Client
    object by running a select query from the database I still get the same
    exception.

    The only work around that seems to work is if I manually retrieve the
    value of the primary key after creating the client object using the
    ObjectId class and set it as one of the fields in the new booking record
    and then commit the booking record to the database.

    Once again I apologise for the simplicity of the question.

    Gary



    This archive was generated by hypermail 2.0.0 : Thu Nov 27 2003 - 02:17:27 EST