You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Sven Woltmann <sv...@gameabo.de> on 2004/07/29 02:00:00 UTC

Problem (Bug?): Inheritance / Mapping Classes on Multiple Joined Tables / Primary Key is _NOT_ auto-increment

Hello,

I have a problem with an inheritance hierarchy, mapped on multiple
joined tables, where the primary key is _not_ auto-increment.  The
problem is:  OJB always stores "0" as the value for the primary key
field, although I defined another value.

--------

To make this easily reproducable, I created the tables from the OJB
guide example:

    CREATE TABLE a (
           id INT NOT NULL,
           some_value_from_a INT,
           PRIMARY KEY (id)
    )TYPE=InnoDB;
    
    CREATE TABLE b (
           id INT NOT NULL,
           some_value_from_b INT,
           PRIMARY KEY (id),
           INDEX (id),
           CONSTRAINT fk_b_a FOREIGN KEY (id)
                      REFERENCES a (id)
    )TYPE=InnoDB;
    
--------

    The classes A and B are defined as follows:

    public class A
    {
        private int id;
        private int someValueFromA;
        // getters and setters...
    }
    
    public class B extends A
    {
        private int id; // actually this field is not 
                        // necessary,  it makes no difference 
                        // to the result if it is there or not
        private int someValueFromB;
        // getters and setters...
    }
    
--------

repository_user.xml defines:

    <class-descriptor
        class="A" 
        table="a"
    >
        <field-descriptor 
            name="id" 
            column="id" 
            jdbc-type="INTEGER" 
            primarykey="true"
        />
        <field-descriptor 
            name="someValueFromA"
            column="some_value_from_a" 
            jdbc-type="INTEGER" 
        />
    </class-descriptor>
    
    <class-descriptor
        class="B" 
        table="b"
    >
        <field-descriptor 
            name="id" 
            column="id" 
            jdbc-type="INTEGER" 
            primarykey="true"
        />
        <field-descriptor 
            name="someValueFromB"
            column="some_value_from_b" 
            jdbc-type="INTEGER" 
        />
        <reference-descriptor name="super"
            class-ref="A" 
            auto-retrieve="true" 
            auto-update="true"
            auto-delete="true" 
        >
            <foreignkey field-ref="id"/>
        </reference-descriptor>
    </class-descriptor>

--------

All this is exactly as in the OJB reference guide - except that the
primary key is _not_ auto-increment (if it _is_ auto-increment,
everything works fine).

I try to create an instance of B and store it:

    B b = new B();
    b.setId(55);
    b.setSomeValueFromA(10);
    b.setSomeValueFromB(20);
        
    broker.store(b);

After the first run, I check the database and I find out that both
records, A and B, have been stored (which means that OJB has understood
that A is a base class of B), but both records have an ID of 0 (zero):

    mysql> select * from a;
    +-----+-------------------+
    | id  | some_value_from_a |
    +-----+-------------------+
    |   0 |                10 |
    +-----+-------------------+
    1 row in set (0.00 sec)
    
    mysql> select * from b;
    +-----+-------------------+
    | id  | some_value_from_b |
    +-----+-------------------+
    |   0 |                20 |
    +-----+-------------------+
    1 row in set (0.00 sec)

Consequently, when executing the code a second time, I get:

    "Duplicate entry '0' for key 1"

This is what my database query log says about it:

First run:
    SELECT id FROM accountingbase.b WHERE id = 55
    INSERT INTO accountingbase.a (id,some_value_from_a) VALUES (0,10)
    INSERT INTO accountingbase.b (id,some_value_from_b) VALUES (0,20)

Second run:
    SELECT id FROM accountingbase.b WHERE id = 55
    INSERT INTO accountingbase.a (id,some_value_from_a) VALUES (0,10)
    ==> "Duplicate entry '0' for key 1"

So, first it checks if no record with ID 55 exists.  And then it tries
to insert a record, but with the ID 0.  So what has happened in between?
How did my 55 become a 0?  Is this a bug in OJB?

Has anyone ever had the same problem?  Can someone help me here?

Thanks in advance and best regards,
Sven

P.S.: I am using OJB version 1.0.0 (final)


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org