On Tuesday, January 21, 2003 at 09:27:59 (-0600) Gelhar, Wallace J. writes:
>Bill,
>
>You are trying to treat the name column as a PK which it is not.  If you
>assign the same food object (same object id and PK) to multiple objects
>through a M:N association OJB will only store on row in the food table.
>
>The "solution" you are looking for is to look up the food object in the
>table (or create with PK) prior to assigning the food to a person.

Thanks Wallace.  Is there no way to make the name column a PK and have
this work 'automatically'?  When I tried this, I was getting errors of
the sort 'duplicate key', which indicates that OJB is trying to do the
insert without determining if it needs to.

The problem is, I have an intermediate table that also needs to be
updated.  I need to associate the Fish to the Gourmet.

So, the pseudo code that would handle the cases I need might look like
this:

    // get a fish, might already exist in DB
    Fish fish = get_fish();

    // get a gourmet, it also might already exist,
    // and it may or may not already be associated with
    // the fish above.
    Gourmet gourmet = get_gourmet();

    if (fish_is_not_in_DB) {
        if (gourmet_not_in_DB) {

            // neither fish nor gourmet is in db, so we just store
            gourmet.add_fish(fish);
            store(gourmet);
        } else {
            // the gourmet is already in the db but not the fish
            // (and therefore, the gourmet is not associated with this
            // new fish)
            store(fish);
            // now, what do I do here? How do I create the relationship
            // between the newly stored fish and the gourmet already
            // in the DB?
    } else {
        if (gourmet_not_in_DB) {
            // The fish is already in the db but not the gourmet
            // and also again, therefore, the association
            store(gourmet);
            // how do I associate the new gourmet and the existing
            // fish?
        } else {
            // here, both gourmet and fish are already in the
            // DB.  It is possible that they are NOT already
            // associated with one another through the intermediate
            // table.

            if (gourmet_is_NOT_associated_with_fish) {
                // perform association
            } else {
                // do nothing, we have both gourmet and fish and
                // they are properly associated
            }
        }
    }

I don't mean to complain, but it seems like it should be possible to
simply do:

    Fish fish = get_fish();
    Gourmet gourmet = get_gourmet();
    gourmet.add_fish(fish);
    store(gourmet);

with OJB doing the work outlined above.  Is this unreasonable?  I'm
certainly willing to be told so and to do the work myself.  Since I'm
new at the O/R concept, I'm just doing the 'seat-of-the-pants' thing
that I naively assume works.  If I simply don't understand the limits
of O/R (or OJB) then I need to learn them.

Thank you again for your help.


Bill

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to