Mapping self-table one-to-many using non-PK clolumns
- by Harel Moshe
Hey, i have a legacy DB to which a Person object is mapped, having a collection of family-members, like this:
class Person
{
...
string Id; /* 9-digits string */
IList<Person> Family;
...
}
The PERSON table seems like:
Id: CHAR(9), PK
FamilyId: INT, NOT NULL
and several other non-relevant columns.
I'm trying to map the Family collection to the PERSON table using the FamilyId column, which is not the PK as mentioned above.
So, i actually have a one-to-many which is self-table-referential.
I'm getting an error saying 'Cast is not valid' when my mapping looks like this:
...
<set name="Family" table="Person" lazy="false">
<key column="FamilyId" />
<one-to-many class="Person" />
</set>
...
because obviously, the join NHibernate is trying to make is between the PK column, Id, and the 'secondary' column, FamilyId, instead of joining the FamilyId column to itself.
Any ideas please?