Let's say your have the following table structure:
==============================
| Case |
==============================
| Id | int |
| ReferralType | varchar(10) |
+---------| ReferralId | int |---------+
| ============================== |
| | |
| | |
====================== ====================== ======================
| SourceA | | SourceB | | SourceC |
====================== ====================== ======================
| Id | int | | Id | int | | Id | int |
| Name | varchar(50) | | Name | varchar(50) | | Name | varchar(50) |
====================== ====================== ======================
Based on the ReferralType the ReferralId contains id to the SourceA, SourceB, or SourceC
I'm trying to figure out how to map this using Fluent NHibernate or just plain NHibernate into an object model. I've tried a bunch of different things but I haven't been succesful. Any ideas?
The object model might be something like:
public class Case
{
public int Id { get; set; }
public Referral { get; set; }
}
public class Referral
{
public string Type { get; set; }
public int Id { get; set; }
public string Name { get; set; }
}
many-to-any
If the table structure is fixed, and you want to use the
identity
id generator, I would map the Source tables as 3 separate classes and map to the common interface as an any reference.union-subclass
Another option is union-subclass with an abstract base class mapping. This allows eager fetching, but you cannot use the
identity
generator on the subclass tables.subclass
If you can change the tables, you can map all 3 Referral classes to the same table using subclass.
I managed to get it working by doing the following: