I am trying to map an class which two other classes will inherit from, i wish to make it abstract so i will be able to return a list which containing two type of child object and cast them accordingly with a discriminator in the parent class.
For an example of my codes, BD looks like the following:
Here are my Domain models:
public class Person
{
public int id {get; set;}
public string name{get; set;}
}
public class Teacher : Person
{
public string subject{get; set;}
}
public class Student : Person
{
public string grade{get; set;}
}
And my Nhibernate mapping:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="Domain.Model" assembly="Domain" xmlns="urn:nhibernate-mapping-2.2" auto-import="false">
<class name="Person" table="`Person`" schema="`PR`">
<id name="id" access="property" column="`id`">
<generator class="identity"/>
</id>
<property name="id" type="int" column="`id`" />
<property name="name" type="String" column="`name`" />
<joined-subclass name="Teacher" table="`Teacher`" schema="`PR`">
<key column="`id`" />
<property name="subject" type="String" column="`subject`" />
</joined-subclass>
<joined-subclass name="Student" table="`Student`" schema="`PR`">
<key column="`id`" />
<property name="grade" type="String" column="`grade`" />
</joined-subclass>
</class>
</hibernate-mapping>
I wish to make my "Person" class abstract so I will be able to have a list that can contain both child type but I'm not sure how to do it
Polymorphism does not require the base class to be abstract. You can work on a List<Person>
containing Teacher
and Student
instances without having Person
being abstract.
Setting Person
as abstract in code only forbid to create a Person
instance.
Setting Person
as abstract in NHibernate mapping with a 'table per subclass' model tells to NHibernate that the Person
table must have a corresponding entry in one of the subclass table. If your DB contain some rows in Person
without any corresponding row in Student
or Teacher
, then this is an error.
Maybe this blog could help better understand what you should do.
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.