-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How do I create an association to a joined-subclass Entity
PostPosted: Tue Nov 21, 2006 6:35 pm 
Newbie

Joined: Fri Apr 07, 2006 7:23 pm
Posts: 9
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.