-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mixed inheritance and bi-directional mapping of subclasses
PostPosted: Thu Mar 01, 2007 1:45 pm 
Newbie

Joined: Fri Nov 03, 2006 5:21 am
Posts: 14
Hibernate version: 3.2.2

Hi folks,

I have a problem regarding the mixed inheritannce strategy.

Consider the following inheritance situation:

Code:
         Parent
            ^
            |
    -------------------
    |                 |
  Child1  1 --- n   Child2


I.e. Child1 and Child2 inherit from Parent and there exists a 1 to n relationship between Child1 and Child2.

I'm using the mixed inheritance startegy where I have three tables PARENT, CHILD11 and CHILD2. The mapping document looks basically like this (I omitted the properties for simplicity):

Code:
<hibernate-mapping>
    <class name="Parent" table="PARENT">

      <subclass name="Child1" discriminator-value="child1">
         <set name="children2" cascade="all,delete-orphan" inverse="true">
            <key column="FK_CHILD1"/>
            <one-to-many class="Child2"/>
         </set>
         <join table="CHILD1">
             <key column="CHILD1_ID"/>
             <!-- properties -->
         </join>
      <subclass>

      <subclass name="Child2" discriminator-value="child2">
         <join table="CHILD2">
             <key column="CHILD2_ID"/>
             <many-to-one name="child1"
                column="FK_CHILD1"
                class="Child1"/>
         </join>
      <subclass>

   </class>
</hibernate-mapping>


My problem is now, that the set association from Child1 to Child2 searches for the key column "FK_CHILD1" in the PARENT table and not in the joined CHILD2 table (the association from Child2 to Child1 works fine).

Is there any way to tell Hibernate, that this "children2" association has to find its foreign key in the joined table "CHILD2" instead of the PARENT table?

I already tried to specify the "table" attribute on the set or use a "foreign-key" attribute instead of the "column" attribute in the key element of the set. However, nothing worked :-(

Thanks in advance,
Joern


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 02, 2007 11:30 am 
Newbie

Joined: Fri Nov 03, 2006 5:21 am
Posts: 14
Hi,

I found a solution (or a workaround to be more precise) for my problem.
I added a custom loader to my mappings for retrieving the collection (I've marked the adde dlines wiht *** at the beginning):

Code:
<hibernate-mapping>
    <class name="Parent" table="PARENT">

      <subclass name="Child1" discriminator-value="child1">
         <set name="children2" cascade="all,delete-orphan" inverse="true">
            <key column="FK_CHILD1"/>
            <one-to-many class="Child2"/>
***         <loader query-ref="children2" />
         </set>
         <join table="CHILD1">
             <key column="CHILD1_ID"/>
             <!-- properties -->
         </join>
      <subclass>

      <subclass name="Child2" discriminator-value="child2">
         <join table="CHILD2">
             <key column="CHILD2_ID"/>
             <many-to-one name="child1"
                column="FK_CHILD1"
                class="Child1"/>
         </join>
      <subclass>

   </class>

***  <sql-query name="children2">
***       <load-collection alias="children2" role="Child1.children2" />
***       select
***         *
***       from
***           PARENT parent,
***           CHILD2 child2
***       where
***           parent.ID = child2.CHILD2_ID
***           and child2.FK_CHILD1 = :id
***   </sql-query>

</hibernate-mapping>



Wow, getting this loader up and running correctly took me a whole day ...

If someone knows another solution just let me know.

Regards,
Joern


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 13, 2007 9:14 am 
Newbie

Joined: Wed Jun 13, 2007 8:30 am
Posts: 1
I'm in a similar situation (mixed inheritannce with a table for every subclass). Reading around the forum, and confirmed in this post, seems implicit that the parent table take every foreign key specified in their subclasses.

I'm using org.hibernate.tool.hbm2ddl.SchemaExport to generate the DDL sql instructions and with a equivalent mapping i obtain the parent table containing the foreign key of their subclasses.

Hibernate, you noticed, look at the foreign key in the parent table, this means it expect to be there, so i argue that's not a SQL generator error/bug.

This is imho the main issue... why i must have the foreign key of a subclass in the paren too?

Please, really, please (not ironic, it's a really please) sombody explain me that.

- It's a bug?
- It's "by design"?
- There's a way to change this behaviour?

Tnx in advance...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.