Hi,

I have a problem trying to persist two objects that are related through an indirection 
table. This is what my tables look like:

-----
PERSON
id
firstname
lastname
...

ADDRESS
id
street
housenr
...

PERSON_ADDRESS
person_id
address_id
-----


I have chosen for this construction, because COMPANY can have multiple addresses too, 
so I don't want a column person_id in ADDRESS. One address doesn't have to be 
available for multiple PERSON's or COMPANY's or whatever, so it actually is more of a 
1:n relationship using an indirection table. So PERSON_ADDRESS will often contain the 
same person_id multiple times, but not the same address_id.
I've read the tutorials, but I don't get it to work. I'll show (a fragment of) our 
repository_user.xml:


-----
<class-descriptor
    class="nl.bergland.codamo.Address"
    table="bit_Address"
>
    <field-descriptor
        name="id"
        column="id"
        jdbc-type="INTEGER"
        primarykey="true"
        length="11"
    >
    </field-descriptor>
    <field-descriptor
        name="street"
        column="street"
        jdbc-type="VARCHAR"
        length="64"
    >
    </field-descriptor>
    <field-descriptor
        name="houseNr"
        column="houseNr"
        jdbc-type="VARCHAR"
        length="16"
    >
    </field-descriptor>
</class-descriptor>

<class-descriptor
    class="nl.bergland.codamo.Person"
    table="bit_Person"
>
    <field-descriptor
        name="id"
        column="id"
        jdbc-type="INTEGER"
        primarykey="true"
        length="11"
    >
    </field-descriptor>
    <field-descriptor
        name="firstName"
        column="firstName"
        jdbc-type="VARCHAR"
        length="24"
    >
    </field-descriptor>
    <field-descriptor
        name="lastName"
        column="lastName"
        jdbc-type="VARCHAR"
        length="24"
    >
    </field-descriptor>
    <collection-descriptor
        name="addresses"
        element-class-ref="nl.bergland.codamo.Address"
        indirection-table="bit_PersonAddress"
        auto-retrieve="true"
        auto-update="true"
        auto-delete="true"
    >
        <fk-pointing-to-this-class column="personId"/>
        <fk-pointing-to-element-class column="addressId"/>
    </collection-descriptor>
</class-descriptor>
-----


The generated tables in mysql seem to be ok with me (we actually use OjbDoclet to 
generate the repository.xml and the SQL) and insertion of a PERSON works fine. 
However, when we create a new person, add a new Address to it and try to save the 
person, the following happens:

A new record gets created for PERSON fine
A new record gets created for ADDRESS fine, but when I set auto-update to true, 2 get 
created??
A new record gets created for PERSON_ADDRESS, but both the columns person_id and 
address_id are set to 0.

Our save code looks something like this:


-----
        Person person = new Person;
        person.getAddresses().add(new Address());

        Implementation impl = OJB.getInstance();
        Database db = impl.newDatabase();
        
        db.open("default", Database.OPEN_READ_WRITE);        
        Transaction tx = impl.newTransaction();        
        tx.begin();
        
        // establish database locks on all root dataobjects
        tx.lock(person, Transaction.WRITE);
        

        // set person and address values
        // ...
        
        tx.commit();
        db.close();
-----


A lot of info for a first-time post, I hope this problem sounds familiar to someone 
here.
Thanks,

-Stijn

Reply via email to