FK mystery deepens

From: Jim Menard (jim..o.com)
Date: Thu Jan 29 2004 - 12:41:37 EST

  • Next message: Jim Menard: "Re: FK mystery deepens"

    OK, I must be doing something really, really stupid. I'm now looking at
    the SQL generated when saving my objects. The id numbers don't match
    up. Here is the Java and the generated SQL. First my simple test case:

         Member member = new Member();
            // ...set some values like name...

            // I don't call createAndRegisterNewObject because my app creates the
            // object elsewhere.
         context.registerNewObject(member);

         BillAccount account =
            (BillAccount)context.createAndRegisterNewObject("BillAccount");
            // ...set some values...

         Address address =
            (Address)context.createAndRegisterNewObject("Address");
            // ...set some values...

         Address billAddress =
            (Address)context.createAndRegisterNewObject("Address");
            // copyInto() copies city, state, postal code but not any relationships
         address.copyInto(billAddress);
         account.setBillingAddress(billAddress);

         member.setBillAccount(account);
         member.addToAddresses(address);
         address.setMember(member); // Is this necessary

         context.commitChanges();

    Here is the generated SQL. I expected the billAddress to be stored
    first because the billAccount has a non-null FK to address. Next, I
    expected the Member to be stored because its address has a (nullable)
    FK that points back to the Address.

    Note that the bill_account.id (281) is NOT THE SAME as the
    member.bill_account_id (180). Why, oh why is this true?

    INFO [12:31:28,270] QueryLogger: INSERT INTO public.bill_account
    (bill_plan_id, billing_address_id, close_date, comment, contact_name,
    email, extern_code, fax, id, open_date, organization_division,
    organization_name, phone1, phone2, status) VALUES (?, ?, ?, ?, ?, ?, ?,
    ?, ?, ?, ?, ?, ?, ?, ?)
    INFO [12:31:28,303] QueryLogger: [bind: NULL, 281, NULL, 'Registered
    at Thu Jan 29 12:31:27 EST 2004 by Jim Menard', 'Jim Menard',
    'jim..o.com', NULL, NULL, 180, '2004-01-29 12:31:27.41', NULL, NULL,
    NULL, NULL, 'ACT']
    INFO [12:31:28,312] QueryLogger: === updated 1 row.
    INFO [12:31:28,313] QueryLogger: INSERT INTO public.address (address1,
    address2, city, country_abbrev, id, member_id, postal_code, state,
    type_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
    INFO [12:31:28,314] QueryLogger: [bind: '1000 Old Post Road', NULL,
    'Fairfield', 'USA', 281, NULL, '06430', 'CT', 2]
    INFO [12:31:28,351] QueryLogger: === updated 1 row.
    INFO [12:31:28,351] QueryLogger: [bind: '1000 Old Post Road', NULL,
    'Fairfield', 'USA', 282, 180, '06430', 'CT', NULL]
    INFO [12:31:28,354] QueryLogger: === updated 1 row.
    INFO [12:31:28,355] QueryLogger: INSERT INTO public.member (active,
    bill_account_id, email, extern_code, first_name, id, last_name,
    middle_name, password, password_hint, phone, preferred_language_id,
    username) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    INFO [12:31:28,356] QueryLogger: [bind: 'true', 180, 'jim..o.com',
    NULL, 'Jim', 180, 'Menard', NULL, NULL, NULL, NULL, NULL, 'jimm']
    INFO [12:31:28,388] QueryLogger: === updated 1 row.
    INFO [12:31:28,391] QueryLogger: +++ transaction committed.

    Help! (And thank you for your help.)

    Jim

    -- 
    Jim Menard, jim..o.com, http://www.io.com/~jimm/
    "The reason why there is no good commercial Java development
    environment is that the only folks that are good enough to write
    one all use EMACS." -- Unknown, on comp.lang.java.programmer
    



    This archive was generated by hypermail 2.0.0 : Thu Jan 29 2004 - 12:41:44 EST