-->
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.  [ 7 posts ] 
Author Message
 Post subject: Using a one to many reference to a joined-subclass entity
PostPosted: Wed Nov 22, 2006 12:30 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  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 1:56 pm 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
I think your problem is in the definition of sets "deers" and "bears":

<set name="bears" inverse="true" lazy="true" cascade="all">
<key column="forest_id" not-null="true" />
<one-to-many class="Bear" />
</set>

if you say "<key column="forest_id"" hibernate will create such key in the Bear table.

You should define a "animals" set:

<set name="animals" inverse="true" lazy="true" cascade="all">
<key column="forest_id" not-null="true" />
<one-to-many class="Animal" />
</set>


To get the "deers" and the "bears" maybe you should use the "table per subclass with discriminator" and use the discriminator in a "select" attribute on your sets...

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 2:33 pm 
Newbie

Joined: Fri Apr 07, 2006 7:23 pm
Posts: 9
I understand that that would force hbm2ddl into submission at the expense of my having to sort through all the different sub-types but I'm trying to avoid having the "animals" set since that would give me all the chipmunks squirrles and other animals that I don't care about (that I omitted from my sample hbm above). I orignally tried to use the discriminator model but that wouldn't let me include sets inside the derived classes (e.g. the Bears class could not contain a set of Dens that it frequented) so I was forced into the joined-subclass model.

Given that hibernate core seems to be doing the right thing (it works fine if I manually edit the SQL schema) I'm inclined to believe that what I'm doing isn't way off base and that I either have a subtle error in my mapping files or I'm dealing with a bug in the hbm2ddl tool.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 12:20 pm 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
Quote:
Given that hibernate core seems to be doing the right thing (it works fine if I manually edit the SQL schema)


What do you mean? If you manually edit the SQL schema, dropping those forest_id, i guess hibernate will do the inserts OK. But, will it give you the Deers of a Forest when you call forest.getDeers()?

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 26, 2006 1:03 pm 
Newbie

Joined: Fri Apr 07, 2006 7:23 pm
Posts: 9
Yes, if I remove the forest_id from the Deer and Bear tables (and the corresponding foreign-key contraints). Everything works as expected. the Forest.getDeers() returns just the Deer and Forest.getBears() returns just the Bears. Cascading works just as well.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 12:25 pm 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
mmm... in my case:

Hibernate version 3.2 cr4
MySQL 5.0.27 InnoDB

And if i remove those keys manually, as you say, when i call forest.getDeers() i get:

JDBCExceptionReporter:63 - could not initialize a collection: [joinedsubclass.Forest.deers#3] [select deers0_.forest_id as forest2_1_, deers0_.animal_id as animal1_1_, deers0_.animal_id as id1_0_, deers0_1_.forest_id as forest2_1_0_ from Deer deers0_ inner join Animal deers0_1_ on deers0_.animal_id=deers0_1_.id where deers0_.forest_id=?]
java.sql.SQLException: Unknown column 'deers0_.forest_id' in 'field list'

What's your DB? and what SQL hibernate generates when you call forest.getDeers()?

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 3:19 pm 
Newbie

Joined: Fri Apr 07, 2006 7:23 pm
Posts: 9
Interesting. Now that I look closer, it was working for me since I was accessing things out of a cache. If i load them from the database, I get the exception as you describe (using PostgreSQL if it makes any difference). So it does populate the database "correctly" (at least as far as I'm concerned) but it does not query properly.

So I guess I'm stuck moving the many-to-one declarations into the joined-subclass definitions, repeating them for each one. Not pretty (and rather redundant) but it seems to work.

Thanks for the help!


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

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.