-->
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.  [ 5 posts ] 
Author Message
 Post subject: HowTo/Best Practices with Inheritance and Relations
PostPosted: Mon Jan 30, 2006 3:17 pm 
Newbie

Joined: Mon Jan 30, 2006 2:26 pm
Posts: 9
Location: Germany
Hi,

assuming I want to have a base class (BaseContent) in a CMS from wich I want to derive all other content classes (Article, ForumPost, Whatever) AND want to maintain relations using this base class, for example to categories and such.

How would I implement this best using NHibernate?

Thanks for any help!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 30, 2006 6:00 pm 
Beginner
Beginner

Joined: Mon Nov 21, 2005 6:38 pm
Posts: 30
Location: New Zealand
This sounds similar to our class hierarchy, where we have a base class and other classes extend that base class. We have mapped our classes using sub-classes, e.g.

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="Domain.Model.Definition, Domain.Model" table="Definition" discriminator-value="Definition">
    <meta attribute="scope-class" inherit="false">public abstract</meta>
    <id name="Id" type="Persistence.NullGuidUserType, Persistence" column="Id">
      <generator class="assigned" />
    </id>
    <discriminator column="DEFINITION_TYPE" type="string" />
    <version name="Version" column="Version" type="Int32" unsaved-value="-1" />
   <subclass name="Domain.Model.ComputedAttributeDefinition, Domain.Model">
      <property name="AttributeDefinitionId" type="Persistence.NullGuidUserType, Persistence" column="AttributeDefinitionId"/>
   </subclass>
   <subclass name ="Domain.Model.ItemAttributeDefinition, Domain.Model" >
      <property name="ItemDefinitionId" type="Persistence.NullGuidUserType, Persistence" column="ItemDefinitionId"/>
   </subclass>
  </class>
</hibernate-mapping>


We have decided to use Table per class hierarchy, hence the use of a discriminator, but you can obviously choose table per sub-class if that suits you better.

We have also marked the base class as public abstract, as it is abstract ;), but if your base class can be instantiated then you obviously don't want to use that.

I recommend you get a copy of Hibernate In Action for a better expalantion than what I've given.

Just be aware that this book requires *very* careful attention when reading it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 5:19 am 
Newbie

Joined: Mon Jan 30, 2006 2:26 pm
Posts: 9
Location: Germany
Alright, thanks for the hint!

Regarding relations, can I easily maintain common ones over the base class?

Let's say, the base table has a many-to-many relation to a categories table,
are those relations available for subclasses as well and how would I set this up?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 7:27 pm 
Senior
Senior

Joined: Sat May 14, 2005 8:40 am
Posts: 130
I have a slight feeling that we're currently thinking about the same concrete problem ;).

Personally I have pretty good experiences with table-per-class mappings (joined subclasses). A table per class hierarchy is impossible, because we're talking about a modular application where every module can introduce its own subclasses with specific attributes.
Another option is to use one table per subclass but that would introduce many duplicate columns in those table, not to mention the fact that you have to map relations to the same classes over and over again.

The only drawback of joined subclasses might be the performance because to getting an instance of a subclass always requires a join. Haven't experienced this as a problem yet.

_________________
Cuyahoga


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 1:53 pm 
Newbie

Joined: Mon Jan 30, 2006 2:26 pm
Posts: 9
Location: Germany
I see that joined subclasses is the way to go.

Thanks for the help (still reading NHibernate docs ;-))


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