-->
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.  [ 6 posts ] 
Author Message
 Post subject: Inheritance mapping - mixing subclass with joined-subclass
PostPosted: Sun Jun 19, 2005 2:59 pm 
Beginner
Beginner

Joined: Sun Jun 19, 2005 2:21 pm
Posts: 21
Hi,

I have three clasess (BaseClass, SmallClass and ExtendedClass) which I need to map using two different mapping strategies: table-per-class hierarchy and table-per-subclass

BaseClass is mapped using a discriminator field and SmallClass is a sub-class of BaseClass mapped with a certain discriminator value. I want to map ExtendedClass (which derives from BaseClass) to a different table as a joined-subclass.

NHibernate let's me do this configuration but when I try to insert records of SmallClass the discriminator column does not get updated and everything in that column is null. I checked the logging information from NHibernate and it seems that the framework does not generate a value for discriminator.

How can I make mapping of this hierarchy of classes work correctly?

Regards,
Robert


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 5:14 am 
Beginner
Beginner

Joined: Sun Jun 19, 2005 2:21 pm
Posts: 21
Can someone suggest a solution please?

At least say that is or is not possible to mix these two types of inheritance in the current version of NHibernate...

Regards,
Robert


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 1:54 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Can you post your code and mapping files ?

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 22, 2005 8:59 am 
Beginner
Beginner

Joined: Sun Jun 19, 2005 2:21 pm
Posts: 21
Code:
BaseClass.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-access="property">
   <class name="Core.Services.BaseClass, Core.Services" table="Base_Object">
      <id name="ProviderUserKey" column="Object_Id" access="nosetter.camelcase" unsaved-value="0">
         <generator class="sequence">
            <param name="sequence">object_id_seq</param>
         </generator>
      </id>
      <discriminator column="Object_Type_ID" />
      <property name="Name" column="Object_Name" not-null="true" />
      <property name="Description" column="Description" length="255"/>
   </class>
</hibernate-mapping>

SmallClass.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-access="property">
   <subclass name="Core.Services.SmallClass, Core.Services"
      extends="Core.Services.BaseClass, Core.Services"
      discriminator-value="4">
   </subclass>
</hibernate-mapping>


ExtentedClass.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-access="property">
   <joined-subclass name="Core.Services.ExtendedClass, Core.Services" extends="Core.Services.BaseClass, Core.Services" table="Extended_Class" dynamic-update="true">
      <key column="Object_ID"/>
      <property name="LastActivityOn" column="Last_Activity" access="nosetter.camelcase" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate"/>
      <property name="PendingRegistration" column="Pending_Registration"/>
   </joined-subclass>
</hibernate-mapping>


And now the classes hierarchy:
Code:
using System;

namespace Core.Services {


   public class BaseClass {
      public string Description {
         get {
            return description;
         }
         set {
              description = value;

         }
      }
      private string description;

      public string Name {
         get {
            return name;
         }
         set {

              name = value;

         }
      }
      private string name;

      public int ProviderUserKey {
         get {
            return providerUserKey;
         }
      }
      private int providerUserKey;
   }

   public class SmallClass: BaseClass {
   }

   public class ExtendedClass: BaseClass {
      public DateTime LastActivityOn {
         get {
            return lastActivityOn;
         }
         set {

            if (lastActivityOn != value) {
              lastActivityOn = value;
            }

         }
      }
      private DateTime lastActivityOn;

      public bool PendingRegistration {
         get {

            return pendingRegistration;
         }
         set {

            if (pendingRegistration != value) {
              pendingRegistration = value;
            }

         }
      }
      private bool pendingRegistration;
   }
}


The class ExtendedClass will always have many more fields than SmallClass and I want it to be persisted in table Extended_Class.

The problem is that NHibernate never populates discriminator column Object_Type_ID when this combination of <joined-subclass> and <subclass> is used.

I hope I managed to express cleary the issue I'm having.

Regards,
Robert


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 8:08 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Your <class name="BaseClass"> doesn't have a discriminator-value="..."; let me know if it solve your problem (and send your log.txt if not)

And to use the table "Extended_Class", you must specify it (by default, it will use "ExtendedClass")

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 5:18 pm 
Beginner
Beginner

Joined: Sun Jun 19, 2005 2:21 pm
Posts: 21
BaseClass is never persisted as itself. It is always inherited so will never end up in the database.

As regarding the Extended_Class table, this is already present as an attribute in <joined-class> tag - table="Extended_Class"...

Thanks for your opinions but this does not solve my problem.

It seems that the only alternatives are:

1. either map all classes to the same table introducing a lot of nullable columns;
2. use table-per-subclass strategy for all derived classes even if I don't need it and expect Hibernate 3 implementation in .NET to use <join> tag.

Rather sad... :-(
Robert


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