Hibernate version:3.2
Hi,
I am attempting to map an existing database using hibernate. The database models the following structure
Code:
Class C extends Class B
Class B extends Class A
It implements this in this table structure
Code:
Table A primary key of A_ID(Identity - Auto Generated)
Table B primary key of B_ID(Identity - Auto Generated) + foreign key of A_ID to Table A
Table C primary key of C_ID(Identity - Auto Generated) + foreign key of B_ID to Table B
This is in keeping with the rest of the database that gives a unique id (primary key) for every table.
In trying to map this using Hibernate, I have chosen the "Table per Subclass" strategy. I have attempted various mappings, but not yet managed to successfully.
Here is one that I have tried.
Code:
<class name="ClassA" table="A"
<id name="id" type="long"
<column name="A_ID" />
<generator class="native"/>
</id>
</class>
<joined-subclass name="ClassB" extends="ClassA">
<key column="B_ID"/>
</joined-subclass>
<joined-subclass name="ClassC" extends="ClassB">
<key column="C_ID"/>
</joined-subclass>
When I use this to perform an insert, the first thing that happens is that an insert to table A happens, and it returns the generated id. It then inserts to Table B, supplying the id obtained from Table A. It then inserts to Table C, but uses the id still from Table A. This causes a foreign key violation on Table C to Table B.
It is almost as if the assumption is that the key used to join to the base class has to be the primary key of the table.
I have tried to put an <id> and a <key> in the <joined-subclass>, but the DTD doesn't allow this. I suspect what I need to do is define the "identity" in the mapping to be independent of the inheritance keys, but I haven't found how to do this.
I am not sure if I have explained this too well. Any pointers and help will be appreciated.