-->
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: Numerous parents to same child class - how to map it?
PostPosted: Wed Mar 08, 2006 8:34 am 
Newbie

Joined: Wed Mar 08, 2006 7:56 am
Posts: 2
Hibernate version: 3.1.2

Name and version of the database you are using: Oracle 9i

I have a scenario like this:
There is a number of parent classes which all have a child of same basic abstract type. For each child I have created a subclass that identifies the type and added a discriminator.

In one of the parents I have this one-to-one mapping:
Code:
<one-to-one name="afdeling" cascade="all" property-ref="parent" class="BehandlingstilbudAfdelingPojo"/>


The children are all specializations of one abstract type called Afdeling.
Code:
  <class table="AFDELING" discriminator-value="AFDELING" name="AfdelingPojo">
    <id name="id">
      <generator class="sequence">
        <param name="sequence">MY_SEQ</param>
      </generator>
    </id>
    <discriminator column="TYPE"/>
...
  </class>


In one of the child subclasses I have:
Code:
<subclass name="BehandlingstilbudAfdelingPojo" discriminator-value="BEHANDLINGSTILBUD">
      <many-to-one not-null="false" unique="true" column="BEHANDLINGSTILBUD_ID" foreign-key="BEHANDLINGSTILBUDAFDLING_CONS" cascade="all" name="parent" class="BehandlingstilbudPojo"/>
    </subclass>


This works just fine and results in a (child) table schema like this.
----------------------------------------------------------------------------------
| AFDELING |
----------------------------------------------------------------------------------
|ID | TYPE | <columns for each property> | BEHANDLINGSTILBUD_ID

This gives me a link from the child to the parent "BEHANDLINGSTILBUD" which is what I want.

Now, my problem is that I have a design in which there are 11 child-implementations of the abstract child class. Using the approach above this will give me an additional 10 xxxx_ID columns in the AFDELING table. In any given row only one of the 11 xxxx_ID columns will contain an ID, the rest will be null.

Is there a way to collapse all these xxxx_ID columns to parent id's into a single "PARENT_ID" column? I already have the discriminator value column "TYPE" to tell me what type of parent I should join the child to. So I think it should be possible.

I have read everything I could find that the manual had to say about discriminators and their use. I also have the HIA book and has not found an answer in that either.

I have not been able to find what I was looking for, but if you happen to know it is documented somewhere please just point me to the chapter/page and I will be perfectly happy with that solution :)

TIA


Top
 Profile  
 
 Post subject: Try adding abstract inner nodes to your hierarchy
PostPosted: Wed Mar 08, 2006 4:10 pm 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
One way to deal with this situation would be to add an abstract base class to all parents that may have children, add an abstract base class for all children that may have parents, and put the field for the link in the abstract base class for the child pointing to the abstract base class for the parent. Since your IDs are sequence-generated, you don't need to use discriminators in your parent-child joins. In each child you can simply downcast the parent to the corresponding parent's subclass.

Here is an example:

abstract class AbstractParent {
...
}

abstract class AbstractChild {
protected AbstractParent parent;
protected AbstractChild( AbstractParent parent ) {
this.parent = parent;
}
...
}

class ParentA extends AbstractParent {
...
}

class ChildA {
ChildA( ParentA parent ) {
super( parent );
}
ParentA getParent() {
return (ParentA)parent;
}
}

class ParentB extends AbstractParent {
...
}

class ChildB {
ChildB( ParentB parent ) {
super( parent );
}
ParentB getParent() {
return (ParentB)parent;
}
}

The corresponding mapping will be straightforward - it will add a single field to your tables.

I hope this helps.


Top
 Profile  
 
 Post subject: Re: Try adding abstract inner nodes to your hierarchy
PostPosted: Thu Mar 09, 2006 5:20 am 
Newbie

Joined: Wed Mar 08, 2006 7:56 am
Posts: 2
skalinic wrote:
One way to deal with this situation would be to add an abstract base class to all parents that may have children, add an abstract base class for all children that may have parents, and put the field for the link in the abstract base class for the child pointing to the abstract base class for the parent. Since your IDs are sequence-generated, you don't need to use discriminators in your parent-child joins. In each child you can simply downcast the parent to the corresponding parent's subclass.

<SNIP lots of nice code :-)>

I hope this helps.


Thank you very much. I hadn't thought of just downcasting the protected "parent" to the actual parent type - actually it was quite simple to implement in my code since most of it was already there - just had to create the abstractParent and add the casts in my child classes :).

In my case some of my parents were already extending another class so I made the AbstractParent an interface. This works just as well and enables my parents to be able to extend another class.

Nice :)


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.