Hibernate version: 3.2.0
Hibernate tools version: 3.2.0.beta8
In simplified terms I have an object, call it a
Forest, that contains (one to many) other objects that are similar to each other, for example
Deer and
Bear which both extend from the generic class
Animal. I am trying to add to my
Forest mapping document two collection mapping sets: one for the
Deer and one for the
Bear inhabitants of the Forest. Here is the gist of the mapping files:
Code:
<hibernate-mapping>
<class name="Forest">
...
<set name="bears" inverse="true" lazy="true" cascade="all">
<key column="forest_id" not-null="true" />
<one-to-many class="Bear" />
</set>
<set name="deers" inverse="true" lazy="true" cascade="all">
<key column="forest_id" not-null="true" />
<one-to-many class="Deer" />
</set>
</class>
<class name="Animal">
...
<many-to-one name="home" class="Forest" column="forest_id" not-null="true" />
<joined-subclass name="Bear">
<key column="animal_id">
...
</joined-subclass>
<joined-subclass name="Deer">
<key column="animal_id">
...
</joined-subclass>
</class>
</hibernate-mapping>
Note that the many-to-one parent reference is in the Animal class, not the derivant Deer and Bear classes.
The problem I'm having is that the hbm2ddl generation causes the Bear and Deer tables to contain columns for "forest_id" but Hibernate core does not expect that to be the case. Basically, my generated schema looks like this:
Code:
create table Animal (
...
forest_id int8 not null,
);
create table Bear (
...
forest_id int8 not null,
);
create table Deer (
...
forest_id int8 not null,
);
Which has the "forest_id" column in all three tables (instead of just the Animal table where it belongs). Hibernate core seems to have read my mind when interpreting the mapping declaration and does not include values for "forest_id" when inserting new rows into the Bear or Deer tables. This causes a NULL Constraint violation (since it does not supply values for the "not null" forest_id column) and things break.
So, am I doing something wrong or is this a question better suited for the Tools group?