TL;DR version: The code below gives me a “Association references unmapped class” exception when building the SessionFactory. What should be changed in the code to fix it?
Even though Ayende @ Rahien doesn’t recommend using mappings to generic classes in this 2007 article I still had a go at it. I used the approach he mentioned. First the bit that is working:
<class name="Review`1[Person]" table="Review">
<id name="Id" column="ReviewId"><generator class="native" /></id>
<property name="Rating" />
<many-to-one name="Subject" column="PersonId" class="Person" />
</class>
The corresponding generic class looks like this:
public class Review<T> : BaseEntity where T : IReviewable
{
public virtual int Rating { get; set; }
public virtual T Subject { get; set; }
}
This is working, and I can load these Review entities and show them in my MVC views no problem. The nice part is that my Subject will have the correct type when I use it in the controller and view.
However, when trying to map a collection of Review items on my Person class things start to break down. Here’s the mapping I’m currently using:
<class name="Person">
<!-- abbreviated -->
<bag name="Reviews" table="Review">
<key column="PersonId"/>
<one-to-many class="Review`1[Person]" />
<!-- Also tried these:
<one-to-many class="table="Review">
... plus a few variations with fully qualified names ...
-->
</bag>
</class>
With this (abbreviated) class:
public class Person : BaseEntity, IReviewable
{
/* Abbreviated */
public virtual IList<Review<Person>> Reviews { get; set; }
}
This ends with an exception while building the SessionFactory:
Association references unmapped class: ….insert one-to-many.class here….
So the question: What’s the problem here? Is this even possible? Should Ijust give up on mapping generic entities?
Mapping-by-code generated this for me:
So, try with fully-qualified names.