Hi Stijn,

> Is it recommended to use mysql's auto-increment with OJB? If so, what am I
> doing wrong? If not, what other approach might you suggest?
>


hmm, it should work. Your code seems ok. In OJB junit test suite we have one test using Identity column based PK fields. You can find this test in [db-ojb]/src/test/org/.../broker/sequence/NativeIdentifierTest
This test works for hsql and mysql and use SequenceManagerNativeImpl as sequence-manager in jdbc-connection-decriptor (in test the SM was set at runtime). The metadata mappings can be found in repository_junit_meta_seq.xml file.
I never try to store m:n relations using SequenceManagerNativeImpl, but it work for 1:n relation, thus I assume it will work for m:n too.


regards,
Armin

Stijn de Witt wrote:
Ok, I have fixed my versioning problems. I can access the new
PersistenceBroker methods and the code runs. To rule out any website related
sideeffects, and to get more info about what is happening, I have created a
separate test app and set debugging levels more verbose.
The test code looks like this:

-----
    public static void main(String[] args)
    {
        logger.info("main: Starting...");
        PropertyConfigurator.configure(args[0]);

        logger.info("main: Getting persistence broker...");
        PersistenceBroker broker = null;
        try
        {
            broker = PersistenceBrokerFactory.defaultPersistenceBroker();

            logger.info("main: Assigning data to persistent objects...");
            Person person = new Person();
            Address address = new Address();
            person.getAddresses().add(address);

            person.setFirstName("stijn");
            person.setMiddleName("de");
            person.setLastName("Witt");
            address.setStreet("Graafseweg");
            address.setHouseNr("7");

            logger.info("main: Starting transaction...");
            broker.beginTransaction();
            Address tmp;
            Iterator it = person.getAddresses().iterator();

            while (it.hasNext())
            {
                logger.info("main: Store address...");
                broker.store(it.next());
            }

            logger.info("main: Store person...");
            broker.store(person);

            broker.serviceBrokerHelper().link(person, true);
            logger.info("main: Commit transaction...");
            broker.commitTransaction();
        }
        catch(Exception e)
        {
            logger.error("main: ERROR getting persistence broker:");
            logger.error("main: " + e.getMessage());
        }


logger.info("main: Done."); } -----


Here is a fragment of the output:


-----
2004-04-14 11:41:48,500 [main] DEBUG ConnectionFactoryAbstractImpl - Create
new connection using DriverManager: [EMAIL PROTECTED]
2004-04-14 11:41:48,500 [main] DEBUG ConnectionManagerImpl - Request new
connection from ConnectionFactory: [EMAIL PROTECTED]
2004-04-14 11:41:48,500 [main] DEBUG ConnectionManagerImpl - localBegin was
called for con [EMAIL PROTECTED]
2004-04-14 11:41:48,500 [main] DEBUG ConnectionManagerImpl - Try to change
autoCommit state to 'false'
2004-04-14 11:41:48,500 [main] INFO  OjbTester - main: Store address...
2004-04-14 11:41:48,531 [main] DEBUG JdbcAccessImpl - executeInsert :
[EMAIL PROTECTED]
2004-04-14 11:41:48,531 [main] DEBUG SqlGeneratorDefaultImpl - SQL:INSERT
INTO bit_Address (id,street,houseNr,zipCode,city,state,country,type) VALUES
(?,?,?,?,?,?,?,?)
2004-04-14 11:41:48,546 [main] DEBUG JdbcAccessImpl - executeInsert:
[EMAIL PROTECTED]: INSERT INTO bit_Address
(id,street,houseNr,zipCode,city,state,country,type) VALUES
(0,'Graafseweg','7','','','','',null)
2004-04-14 11:41:48,562 [main] DEBUG StatementManager - closeResources was
called
2004-04-14 11:41:48,562 [main] INFO  CacheDistributor - Create new
ObjectCacheImplementation for 'default'
2004-04-14 11:41:48,562 [main] INFO  OjbTester - main: Store person...
2004-04-14 11:41:48,562 [main] DEBUG JdbcAccessImpl - executeInsert :
[EMAIL PROTECTED]
2004-04-14 11:41:48,562 [main] DEBUG SqlGeneratorDefaultImpl - SQL:INSERT
INTO bit_Person
(id,salutation,title,initials,middleName,lastName,firstName,birthDate,nation
ality) VALUES (?,?,?,?,?,?,?,?,?)
2004-04-14 11:41:48,562 [main] DEBUG JdbcAccessImpl - executeInsert:
[EMAIL PROTECTED]: INSERT INTO bit_Person
(id,salutation,title,initials,middleName,lastName,firstName,birthDate,nation
ality) VALUES (0,null,null,null,'de','Witt','stijn',null,null)
2004-04-14 11:41:48,609 [main] DEBUG StatementManager - closeResources was
called
2004-04-14 11:41:48,609 [main] DEBUG SqlGeneratorDefaultImpl - SQL:DELETE
FROM bit_PersonEmailAddress WHERE personId=?
2004-04-14 11:41:48,609 [main] DEBUG JdbcAccessImpl - executeUpdateSQL:
DELETE FROM bit_PersonEmailAddress WHERE personId=?
2004-04-14 11:41:48,640 [main] DEBUG StatementManager - closeResources was
called
2004-04-14 11:41:48,640 [main] DEBUG SqlGeneratorDefaultImpl - SQL:DELETE
FROM bit_PersonPhoneNumber WHERE personId=?
2004-04-14 11:41:48,640 [main] DEBUG JdbcAccessImpl - executeUpdateSQL:
DELETE FROM bit_PersonPhoneNumber WHERE personId=?
2004-04-14 11:41:48,671 [main] DEBUG StatementManager - closeResources was
called
2004-04-14 11:41:48,671 [main] DEBUG JdbcAccessImpl - executeInsert :
[EMAIL PROTECTED]
2004-04-14 11:41:48,687 [main] DEBUG JdbcAccessImpl - executeInsert:
[EMAIL PROTECTED]: INSERT INTO bit_Address
(id,street,houseNr,zipCode,city,state,country,type) VALUES
(0,'Graafseweg','7','','','','',null)
2004-04-14 11:41:48,687 [main] DEBUG StatementManager - closeResources was
called
2004-04-14 11:41:48,687 [main] DEBUG SqlGeneratorDefaultImpl - SQL:SELECT
addressId FROM bit_PersonAddress WHERE personId=?
2004-04-14 11:41:48,687 [main] DEBUG JdbcAccessImpl - executeSQL: SELECT
addressId FROM bit_PersonAddress WHERE personId=?
2004-04-14 11:41:48,750 [main] DEBUG SqlGeneratorDefaultImpl - SQL:DELETE
FROM bit_PersonEmailAddress WHERE personId=?
2004-04-14 11:41:48,750 [main] DEBUG JdbcAccessImpl - executeUpdateSQL:
DELETE FROM bit_PersonEmailAddress WHERE personId=?
2004-04-14 11:41:48,750 [main] DEBUG StatementManager - closeResources was
called
2004-04-14 11:41:48,750 [main] DEBUG SqlGeneratorDefaultImpl - SQL:DELETE
FROM bit_PersonPhoneNumber WHERE personId=?
2004-04-14 11:41:48,750 [main] DEBUG JdbcAccessImpl - executeUpdateSQL:
DELETE FROM bit_PersonPhoneNumber WHERE personId=?
2004-04-14 11:41:48,750 [main] DEBUG StatementManager - closeResources was
called
2004-04-14 11:41:48,750 [main] DEBUG SqlGeneratorDefaultImpl - SQL:INSERT
INTO bit_PersonAddress (personId,addressId) VALUES (?,?)
2004-04-14 11:41:48,750 [main] DEBUG JdbcAccessImpl - executeUpdateSQL:
INSERT INTO bit_PersonAddress (personId,addressId) VALUES (?,?)
2004-04-14 11:41:48,750 [main] DEBUG StatementManager - closeResources was
called
2004-04-14 11:41:48,750 [main] INFO  OjbTester - main: Commit transaction...
2004-04-14 11:41:48,750 [main] DEBUG ConnectionManagerImpl - commit was
called
2004-04-14 11:41:48,750 [main] INFO  OjbTester - main: Done.
-----

Now I see the insert for address passing an id, which looks wrong to me
(since it is an auto-increment column), but it works ok. However, the
generated id is never fetched from the database, so it is no wonder that the
personId and addressId fields in the link table bit_PersonAddress get set to
0!

Is it recommended to use mysql's auto-increment with OJB? If so, what am I
doing wrong? If not, what other approach might you suggest?

Thanks for all your time,
-Stijn


----- Original Message ----- From: "Stijn de Witt" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, April 09, 2004 1:32 PM
Subject: Problems saving a m:n relationship



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


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




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



Reply via email to