Association references unmapped - Collection of objects mapped by table per subclass strategy

2.5k Views Asked by At

I'm having problems with NHibernate mapping and I'm not sure if I made a noob mistake or whether I've run into a limitation of NHibernate

I have three domain objects with associated mapping hbm.xml files
Person
SpecialPerson
PersonCategory

public class Person
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Person"
         table="Person">
    <id name="Id" column="PersonID">
      <generator class="native" />
    </id>
    <property name="Name" />
  </class>
</hibernate-mapping>

SpecialPerson derives from Person

public class SpecialPerson : Person
{
    public virtual string MagicString { get; set; }
    public virtual PersonCategory PersonCategory { get; set; }
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <joined-subclass name="SpecialPerson"
                   extends="Person">
    <key column="PersonID" />
    <property name="MagicString" />
    <many-to-one name="PersonCategory" column="PersonCategoryID" cascade="save-update" />
  </joined-subclass>
</hibernate-mapping>

and PersonCategory holds a collection of SpecialPersons <== this is what's causing me grief

public class PersonCategory
{
    public virtual int Id { get; set; }
    public virtual ICollection<SpecialPerson> Persons { get; set; } 
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="PersonCategory"
         table="PersonCategory">
    <id name="Id" column="PersonCategoryID">
      <generator class="native" />
    </id>
    <set name="Persons" table="SpecialPerson" inverse="true">
      <key column="PersonCategoryID" />
      <one-to-many class="SpecialPerson"/>
    </set>
  </class>
</hibernate-mapping>

When I attempt to create a session, I get an NHibernate.MappingException saying "Association references unmapped class: SpecialPerson"

Either I'm not using the proper syntax, as in I'm not supposed to specify the table attribute but something else when I'm trying to reference a derived type OR
NHibernate doesn't allow an object to hold a collection of a derived type unless that derived type is mapped in a table per concrete class inheritance mapping strategy. Can anyone enlighten me?

1

There are 1 best solutions below

0
On

I was able to find the answer here

So the summary is that this had nothing to do with the fact I was trying to reference a derived type. The class attribute in the set's one-to-many element needed a fully signed name. So this works

<set name="Persons" table="SpecialPerson">
  <key column="PersonCategoryID" />
  <one-to-many class="NHibernateTest.Domain.Test.SpecialPerson, NHibernateTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</set>

I have no idea why it needed a fully signed name in this case, when I have used a simplified name everywhere else without any problems.

So it wasn't a limitation with NHIbernate, which is a great relief as I was contemplating the unsavory task of rewriting a lot of our existing mappings.