-->
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: Inheritance mapping without discriminator
PostPosted: Wed Jun 27, 2007 8:23 am 
Beginner
Beginner

Joined: Wed Nov 29, 2006 12:23 pm
Posts: 42
Is it possible to map two classes (baseclass and subclass) to a single table without the use of a discriminator? if I only have one subclass type, then every record in the table is of type base class and subclass. I need to be able to do this, as the subclass is a very large entity, and the baseclass is very small. I don't always want to be getting the large subclass. I've tried doing this as a mapping file with two classes, but whether I try to get the base type or the subclass type, NHibernate issues two select statments - one for the base class and one for the subclass. When I call List, I end up with a list of every base class and also the same identity in the form of the subclass.

Is this possible? If it's not possible to do this without a discriminator, if I add a discriminator column won't all rows in the table end up with the same value given they are all of both types?


Top
 Profile  
 
 Post subject: Not really an inheritance problem...
PostPosted: Wed Jun 27, 2007 11:54 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
hitch wrote:
they are all of both types?


The statement "the object is of both types" is a non-concept in the OO idiom. If you really an object to function "differently" in a different context, the pattern you are looking for is a role.

However, in your case It looks like you need lazy-loaded properties (which is not currently supported) and built this "inheritance" model as a hack to get similar functionality. So, you really have only one class (the subclass), it's just that you only need to load a subset of the properties most of the time, so you devised a "base" as a container for those properties.

Lazy properties are a very useful feature that isn't easily supported by NH. As far as I know, the only recognized method involves supplying your own ProxyFactory and LazyInitializer, which seems quite advanced (probably more info on Ayende Rahien's blog).

I'd also put up another post here to see what workarounds the community has developed to "spoof" this missing feature.


Top
 Profile  
 
 Post subject: Came up with a workaround
PostPosted: Thu Jan 17, 2008 12:52 pm 
Newbie

Joined: Thu Jan 17, 2008 12:45 pm
Posts: 1
I had the same question but came up with a workaround. I couldn't get it to work as a true subclass, so I created a new hbm with a one-to-one relationship to the object that I was trying to extend.

I did something like this:



Code:
<hibernate-mapping >

    <class name="PersonAttendance" table="People">
        <id name="person_id" column="Person_ID"   type="java.lang.Integer">
            <generator class="assigned"/>
        </id>
       
        <one-to-one name="Person" class="Person" />

      <!-- Here I put all the properties I didn't want cluttering my full Person object -->

</class>
</hibernate-mapping>



Then in your "subclass" you can use decorator methods to access the properties of your "base" class.

Code:
   private boolean isFullTime() {
      return getPerson().isFullTime();
   }



A little hacky but it had the effect that I was trying to acheive :)


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.