I’d like to map a Person object.
I’ve got the People table with PersonId, FatherId and MotherId fields (among others). The two last reference PersonId in the very same table.
In my domain model I’d like to have a Children read-only collection with every other Person having set their FatherId or MotherId to parent’s PersonId.
One way to do that is to use two HasMany and map using FatherId and MotherId foreign keys respectively into FathersChildren and MothersChildren collections and returning one of those in a Children getter depending on the parent’s gender. But that makes my domain object just ugly, so to speak.
Is there any way to solve this just in the mappings? I’d be happy with either fluent or hbm.
Well, I was looking at my model and it came to me that NHibernate needs all properties to be
virtualto allow lazy-loading. It returns an object with a class that inherits from the original. So I did the same.My domain model stays the same:
Entity class for mapping:
And the mapping slightly modified for derived class:
Now I have a clean model!