-->
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.  [ 4 posts ] 
Author Message
 Post subject: table-per-class issue
PostPosted: Mon Jun 05, 2006 1:22 am 
Beginner
Beginner

Joined: Fri Mar 12, 2004 1:02 pm
Posts: 23
Hi,

I have a question about the table-per-class hierarchy. Lets say I have AbstractClassX, AbstractClassY and AbstractClassZ. AbstractClassZ extends AbstractClassY and AbstractClassY extends AbstractClassX. Each abstract class has several concrete implementations as well. For example, there is is ConcreteClassX1, ConcreteClassX2, ConcreteClassY1, etc. Now I'm having difficulty fitting this into the hibernate mapping scheme of things. This is what I want to do but does not work:

##########################
<hibernate-mapping>
<class
name="com.test.domain.AbstractClassX"
table="X"
discriminator-value="AbstractClassX">

<!-- Primary Key -->
<id name="xId" type="long">
<column name="X_ID" not-null="true"/>
<generator class="native"/>
</id>

<!-- discriminator (i.e. table per class hierarchy) -->
<discriminator column="X_TYPE" type="string"/>

<!-- properties for class X -->
<property name="value" column="VALUE" type="string"/>

<!-- subclasses -->

<subclass
name="com.test.domain.AbstractClassY"
discriminator-value="AbstractClassY"/>

<subclass
name="com.test.domain.AbstractClassZ"
discriminator-value="AbstractClassZ">
<property name="description" column="DESC" type="string"/>
</subclass>

</class>
</hibernate-mapping>
############################


I think for the subclasses they must be concrete. Instead of specifying AbstractClassY and Z, I used ConcreteClassX1 and ConcreteClassX2 for the subclasses above. This seemed to work (at least hibernate does not complain). But this is not exactly what I want to do. I don't want to specify all of the concrete classes, just the abstract (sub)classes. The main reason being is that the number of concrete classes can grow but they will all extend from one of the three abstract classes. So what is the best way of modeling this in hibernate? The domain is stuck with this object model but the data model can be anything so if I need to scrap table-per-class hierarchy for something else, I can do this. Thanks in advance!

-los


Top
 Profile  
 
 Post subject: Re: table-per-class issue
PostPosted: Mon Jun 05, 2006 5:27 am 
Beginner
Beginner

Joined: Wed May 17, 2006 3:38 am
Posts: 45
You have 2 options:
1)Table per class heirarchy
2)Table per sub class

In #1, you cannot use Abstract class, since it does not make sense and hibernate do not allow it. Besides, your justification "main reason being is that the number of concrete classes can grow" for not using #1 do not appear to be justified...so what if your mapping file grows as the number of subclasses grow...do you fear your mapping file may grow too big? ...If it becomes bigger, it cannot become that big as to be unmanageable. Besides, that is what declarative style programming is all about whereby one does not have touch the code if any changes occur in the configuration/implementation/domain-model e.t.c

However remember #1, has the problem that "properties of subclass that map to corresponding table" has to be nullable....and if that is not supported as per your scheme of things then you cannot use #1.

In #2, as well, you have to map all subclass using <joined-subclass>, asssuming none of them are value-type.

However, I do not see any option in hibernate that allow mapping to an abstract class....I am sure you are looking for mapping that allows polymorphic queries and associations...the logic is that if you map abstract classes only, how can hibernate support polymorphic stuff...there is no way hibernate can know about the presence of the concerete subclases other than the mapping file....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 8:58 am 
Beginner
Beginner

Joined: Fri Mar 12, 2004 1:02 pm
Posts: 23
Hi,

Thanks for the reply.

>>Besides, that is what declarative style...

I guess you mean using annotations or Xdoclet. We might be going to this later but for now we are hand-coding the mappings.

>> ...concrete classes can grow...

Well, growing is one thing, but repeating all of the properties per concrete class in the mapping is another. For example, each concrete subclass I define for AbstractClassY and Z, I will have to specify the properties (<property name=".../>), and all of these will be repeats.

Is there a way to specify in the mapping a base abstract class where I define these properties and then specify all of the concrete subclasses that inherit from this? Sort of like how Spring does it where you can specify an 'abstract' bean and have all subsequent beans inherit from this via 'parent' attribute. That way I won't have to repeat the same 15 or so properties for each concrete subclass.

Another way I was thinking about was to create separate hibernate mappings for AbstractClassY and AbstractClassZ and have the mapping for AbstractClassX reference them. For example, is something like the one below possible:

#####################################
<hibernate-mapping>

<class
name="com.test.domain.AbstractClassX"
table="X"
discriminator-value="AbstractClassX">

....
....
<!-- subclasses -->

<subclass
ref="AbstractClassY.hbm.xml"/>
<subclass
ref="AbstractClassZ.hbm.xml"/>

</class>

</hibernate-mapping>
#####################################


Thanks in advance!

-los


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 06, 2006 2:59 am 
Beginner
Beginner

Joined: Wed May 17, 2006 3:38 am
Posts: 45
I did not mean annotations or Xdoclet since this is irrelevant to this discussion...I meant the mapping files...they are also part of declarative styleof programming.

For your other questions, I am not sure what exactly troubles you....as for what is possible and not possible in Hibernate, perhaps you need to go through the docs further

moraleslos wrote:
Hi,


I guess you mean using annotations or Xdoclet. We might be going to this later but for now we are hand-coding the mappings.

>> ...concrete classes can grow...


Is there a way to specify in the mapping a base abstract class where I define these properties and then specify all of the concrete subclasses that inherit from this?

-los

_________________
===============================
Rate, if it helped


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