简体   繁体   中英

NHibernate inheritance mapping

I have a set of tables that look like the following:

Person - PersonID, FirstName, LastName, etc...<br/>
Family - FamilyID, etc...<br/>
Parent - PersonID, FamilyID, etc...<br/>
Child -  PersonID, FamilyID, etc...<br/>
Contact- PersonID, etc...<br/>

My object hierarchy is organized as follows (using c#):

class Person
class Family
interface IFamilyMember

class Parent : Person, IFamilyMember
class Child : Person, IFamilyMember
class Contact : Person

Using Nhibernate - I am currently mapping each of the Person subclasses via joined-subclass as shown below (irrelevant fields are removed):

<!-- Parent -->
<joined-subclass name="Parent" table="Parent">
  <key column="PersonID" />
  <many-to-one name="Family" column="FamilyID" cascade="save-update" />
</joined-subclass>

<!-- Child -->
<joined-subclass name="Child" table="Child">
  <key column="PersonID" />
  <many-to-one name="Family" column="FamilyID" cascade="save-update" />
</joined-subclass>

<!-- Child -->
<joined-subclass name="Contact" table="Contact">
  <key column="PersonID" />
</joined-subclass>

The problem is that I would REALLY like to be able to execute queries against the IFamilyMember interface, and with the current mapping, NHibernate does not allow to execute an HQL query such as "From IFamilyMember". I would also like to be able to select by other criteria -- "From IFamilyMember m where m.Name.First = 'blah'. How would I map this set of classes such that I could select family members by name, etc... ? Do I have to modify my class hierarchy such that IFamilyMember contains the properties by which I wish to query (Name, etc...)?

Thanks for the help!

Why are Parent and Child different entities? In just the standard 'family' sense, each is just a Person - the Parent/Child is a relationship that is probably best modeled through a many-to-many relationship expressed in a 2nd table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM