Mapping self-table one-to-many using non-PK clolumns

Posted by Harel Moshe on Stack Overflow See other posts from Stack Overflow or by Harel Moshe
Published on 2009-11-25T20:57:36Z Indexed on 2010/04/12 10:03 UTC
Read the original article Hit count: 386

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?

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about nhibernate-mapping