-->
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.  [ 8 posts ] 
Author Message
 Post subject: Creating concrete class inherited from an abstract class
PostPosted: Thu Jun 01, 2006 11:19 am 
Beginner
Beginner

Joined: Wed Apr 26, 2006 4:12 am
Posts: 29
I have a mapping file and define an abstract class definition based on a table. I would like to simply create another class which inherits from this one simply to override a couple of properties. However, is this possible, and if so, what is the recommended approach?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 11:37 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
in terms of implementing the inheritance in code, i'm assuming you understand all of that. when it comes to the mapping files you have two choices; placing the subclass mappings in the main abstract class mapping file, or separating the subclass mappings into separate files that "extend" the abstract class mapping file.

at a higher-level, though, there are at three basic strategies; table-per-hierarchy, and table-per-subclass, table-per-concrete-class. i personally prefer table-per-subclass because it approaches database normilization standards in a cleaner method. assuming table-per-subclass:

Code:
<hibernate-mapping
   xmlns="urn:nhibernate-mapping-2.0"
   assembly="My.Core"
   namespace="My.Core.Domain">

   <class
      name="AbstractEntity"
      table="AbstractEntity">

      <id
         name="id"
         column="EntityID"
         type="Int32"
         unsaved-value="-1"
         access="field">
         <generator class="identity" />
      </id>
      <version name="version" column="Version" type="Int32" unsaved-value="-1" access="field" />
      
      <property
         name="MyProperty"
         column="MyProperty"
         type="Boolean"
         not-null="true" />
      
   </class>

</hibernate-mapping>


Code:
<hibernate-mapping
   xmlns="urn:nhibernate-mapping-2.0"
   assembly="My.Core"
   namespace="My.Core.Domain">
   
   <joined-subclass
      name="ConcreteEntity"
      extends="My.Core.Domain.AbstractEntity, My.Core"
      table="ConcreteEntity">
      
      <key column="EntityID" />
      
      <property
         name="NewProperty"
         column="NewProperty"
         type="Boolean"
         not-null="true"  />
         
   </joined-subclass>
   
</hibernate-mapping>


this relies on the the two tables sharing the PK->PK,FK relationship.

look here for more detailed information:

http://www.hibernate.org/hib_docs/nhibe ... nheritance

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 11:42 am 
Beginner
Beginner

Joined: Wed Apr 26, 2006 4:12 am
Posts: 29
Basically, I want to simply extend another class but the derived class still refers to the same table as the parent class. Do I simply use the approach you described, and the table name will be the same for both the parent and subclass?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 11:57 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
i see. no, you would be using the table-per-class-hierarchy strategy. in this instance you will have to add a column to your table for the discriminator type of the class and map the file using the <subclass> tag, and yes, you will be storing data from both classes in the same table.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 8:57 am 
Beginner
Beginner

Joined: Wed Apr 26, 2006 4:12 am
Posts: 29
i guess i've hit the kind of restriction I was afraid of here. If all I want to do is redefine some of the parent properties by simple overries in a concrete class, I have to go thru this rigmarole of having to have a discriminator column in order to do a very basic thing.

Now, I did manage to get the behaviour I wanted by simply creating a separate mapping file for my concrete class which only had the properties in it that I was overriding in my parent class. This mapping file had nothing to do with my parent class's mapping file. The only similarity was in the table name, which was the same. this seems to work as when I load a record from the session into my concrete class, i can still persist data using my concrete instance. However, do you think this may be problematic? I need to test this more thoroughly before I can be truly confident this would work.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 9:30 am 
Beginner
Beginner

Joined: Wed Apr 26, 2006 4:12 am
Posts: 29
If I was to use the preferred table-per-class-hierarchy approach and add a dummy discriminator to my table and populate it with one single value for all records, could I trick Hibernate into doing what I want? It seems a shame to have to add a column just to do something like this, but if it allows me to achieve the desired aim, that wouldn't be so bad(altho' getting it past our DBA may require a few beers down the pub!!).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 10:18 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
yes absolutely.

'calculated' discriminators are not supported, although you could always use a view ofcourse.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 11:31 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
Quote:
this seems to work as when I load a record from the session into my concrete class, i can still persist data using my concrete instance. However, do you think this may be problematic?


i don't really see an implementation problem with it at all. NH will simply use the mapping file that is associated with it. NH will not understand that the object is derived from another class but i don't think it needs to.


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