Table Per Class Hierarchy Inheritance Mapping

Paolo Bonacini's Avatar

Paolo Bonacini

25 Mar, 2010 10:02 AM via web

I've got this situation:

abstract ClassBase
ClassA : ClassBase
ClassB : ClassBase

These are may map:

public class ClassBaseMap: ClassMap<ClassBase>
{
    public ClassBaseMap()
    {
        Map(x=>x.....);
        ...
        DiscriminateSubClassesOnColumn<string>("Type");
    }
}

public class ClassAMap: SubclassMap<ClassBase>
{
    public ClassAMap()
    {
        DiscriminatorValue("A");  
     }
}

public class ClassBMap: SubclassMap<ClassBase>
{
    public ClassBMap()
    {
        DiscriminatorValue("B");
    }
}

Now the problem is when I want to persist this class:

public class Container
{
  public virtual IList<ClassBase> Collection { get; set; }
}

I want to exclude the ClassBase. I read I can use IgnoreBase, but I can use it only with AutoMapping and I don't use it.
Any suggestions?

Thanks and sorry for my poor english.
Paolo

  1. Support Staff 2 Posted by James Gregory on 25 Mar, 2010 01:46 PM

    James Gregory's Avatar

    SubclassMaps are only for when you're mapping an inheritance hierarchy that you want representing in your database. If you don't want your base class to be in the database, don't map it.

    You want:

    public class ClassAMap: ClassMap<ClassA>
    public class ClassBMap: ClassMap<ClassB>
    
  2. 3 Posted by Paolo Bonacini on 25 Mar, 2010 02:30 PM

    Paolo Bonacini's Avatar

    It's ok when I save the class Container, but when I get it from database?
    Suppose I want to get an instance of Container class and its IList property. Reading from the same table (because I have just one table), how can NHibernate know if is a ClassA or ClassB instance?

    Thank you.
    Paolo

  3. 4 Posted by Berryl Hesh on 26 Mar, 2010 05:55 PM

    Berryl Hesh's Avatar

    @Paulo

    It can't without some type of polymorphic mapping.

    What happens if you don't exclude the base class from your automapping? Does NHib give you polymorphic behavior when you do exclude it?

    @James

    I'm trying to learn how to best use FNH / NHib too and this seems like a legitimate question to me.

  4. 5 Posted by Berryl Hesh on 28 Mar, 2010 03:45 AM

    Berryl Hesh's Avatar

    @Paulo

    I looked at this again. The way you want to use ClassBase polymorphically as a Collection in your container suggests you do not want to exclude the base class.

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    Insert the next number in this sequence: 10, 11, 12, 13, 14, ??