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: joined-subclass issue
PostPosted: Fri May 04, 2007 2:10 pm 
Beginner
Beginner

Joined: Fri Jul 14, 2006 1:51 pm
Posts: 27
I'm using the table-per-subclass method. I have a base class with 3 subclasses. A base object can be one or more of the subclasses. So I can have an entity with id 11 of each of the subclass types. The subclass is an abstract class with several common properties and the subclasses each have some specialized properties of their own.

If I've loaded subclass 1 id 11 and attempt to load subclass 2 id 11 in EntityKey.Equals(object) it checks for equality by comparing the id (which is obviously equal) and the identifierSpace with is built using the base class type so again these are equal and it it will then throw an objectNotFoundException.

I'm using MappingAttributes so here is the base class attribute:

Code:
[Class(0, NameType=typeof(BaseClass), Lazy=false, Polymorphism = PolymorphismType.Explicit, ProxyType=typeof(BaseClass))]


and each subclass attribute looks like this:
Code:
[JoinedSubclass(0, NameType = typeof(SubClass1), Lazy = false, ProxyType = typeof(SubClass1), ExtendsType = typeof(BaseClass), SelectBeforeUpdate = true)]


Top
 Profile  
 
 Post subject: joined-subclass base type
PostPosted: Mon May 07, 2007 8:24 am 
Beginner
Beginner

Joined: Fri Jul 14, 2006 1:51 pm
Posts: 27
Is there a mapping attribute that would cause the identifierspace to be built using the requested type rather than the base type?


Top
 Profile  
 
 Post subject: more info
PostPosted: Mon May 07, 2007 12:38 pm 
Beginner
Beginner

Joined: Fri Jul 14, 2006 1:51 pm
Posts: 27
Here's a test I tried without the mapping attributes in case that makes it clearer.

Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="NHInheritance.Core.BaseClass, NHInheritance.Core" table="BaseClass" lazy="false">
    <id name="Id" column="ID" type="int">
      <generator class="native" />
    </id>
    <property name="Name" column="Name" type="string" not-null="true" />
    <joined-subclass name="NHInheritance.Core.SubClass1, NHInheritance.Core" table="SubClass1" lazy="false">
      <key column="BaseClassID" />
      <property name="SubClass1Prop" column="SubClass1Prop" type="string" not-null="true" />
      <bag name="OtherEntity" inverse="true" lazy="true" cascade="delete">
        <key column="SubClass1ID" />
        <one-to-many class="NHInheritance.Core.OtherEntity,NHInheritance.Core" />
      </bag>
    </joined-subclass>
    <joined-subclass name="NHInheritance.Core.SubClass2, NHInheritance.Core" table="SubClass2" lazy="false">
      <key column="BaseClassID" />
      <property name="SubClass2Prop" column="SubClass2Prop" type="string" not-null="true" />
      <bag name="OtherEntity" inverse="true" lazy="true" cascade="delete">
        <key column="SubClass2ID" />
        <one-to-many class="NHInheritance.Core.OtherEntity,NHInheritance.Core" />
      </bag>
    </joined-subclass>
    <joined-subclass name="NHInheritance.Core.SubClass3, NHInheritance.Core" table="SubClass3" lazy="false">
      <key column="BaseClassID" />
      <property name="SubClass3Prop" column="SubClass3Prop" type="string" not-null="true" />
      <bag name="OtherEntity" inverse="true" lazy="true" cascade="delete">
        <key column="SubClass3ID" />
        <one-to-many class="NHInheritance.Core.OtherEntity,NHInheritance.Core" />
      </bag>
    </joined-subclass>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="NHInheritance.Core.OtherEntity,NHInheritance.Core" table="OtherEntity" lazy="true">
    <id name="Id" column="ID" type="int">
      <generator class="native" />
    </id>
    <property name="OtherEntityProp1" column="OtherEntityProp1" type="string" not-null="true" />
    <many-to-one name="SubClass1" column="SubClass1ID" cascade="save-update" outer-join="false" not-null="true" />
    <many-to-one name="SubClass2" column="SubClass2ID" cascade="save-update" outer-join="false" not-null="true" />
    <many-to-one name="SubClass3" column="SubClass3ID" cascade="save-update" outer-join="false" not-null="true" />
  </class>
</hibernate-mapping>


If I attempt to then load all SubClass1 objects, then all SubClass2 objects I will get a WrongClassException.

Any ideas?


Top
 Profile  
 
 Post subject: Re: joined-subclass issue
PostPosted: Wed Jun 20, 2007 2:11 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
I'm surprised no one else picked up on this. I found it during a (somewhat) related search:

anortham wrote:
I'm using the table-per-subclass method. I have a base class with 3 subclasses. A base object can be one or more of the subclasses. So I can have an entity with id 11 of each of the subclass types.


The problem is that this is not a consistent OO model, and will not function no matter what you do. The statment "a base object can be one or more of the [parallel] subclasses" cuts directly against the definitions of the concepts "base class" and "subclasses". Essentially, using inheritance forces your objects to "choose" one and only one subclass to implement.

Here is an article that goes into further detail, and presents possible solutions.

Good luck!


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.