inheritance with fluent nhibernate
This question is really to establish if what im trying to achieve is possible.
I have three tables i'll omit the actual names to keep it simple and less confusing
so we have
Person [ PID - uniqueidentifier PK ]
Applicant [ PID - uniqueidentifier PK, AID - varchar(20)]
Student [ PID - uniqueidentifier PK, SID - int]
both the student and applicant tables are related to the person table with a one-to-one relationship (where the primary key is held in the person table), but are not related to each other.
I have the following domain objects
public class Person
{
public virtual Guid PID{get;set;}
}
public class Applicant: Person
{
public virtual string AID {get;set;}
}
public class Student:Person
{
public virtual int SID {get;set;}
}
These each have a map class derived from either ClassMap or SubClassMap
public class PersonMap: ClassMap<Person>
{
public PersonMap()
{
Id(x => x.PID).GeneratedBy.Assigned();
Table("Person");
}
}
public class ApplicantMap: SubclassMap<Applicant>
{
public ApplicantMap()
{
Table("Applicant");
KeyColumn("PID");
Map(x => x.AID);
}
}
public class StudentMap:SubclassMap<Student>
{
public StudentMap()
{
Table("Student");
KeyColumn("PID");
Map(x => x.SID);
}
}
My question is, is it possible to have a person who is an applicant and a student at the same time.In database terms I can have a row in each table that holds the same PID, will nhibernate allow me to save and retrieve all three objects?
one of the problems I have is trying to insert into student when the id already exists for an applicant and of course person..
public void MakePersonAStudent(Person p)
{
Student newStudent = new Student();
newStudent.PID = p.PID;
newStudent.SID = getNewStudentID();
Session.Save(newPerson);
}
The exception generated is:
a different object with the same identifier value was already associated with the sessi开发者_开发百科on .... 73e5fd90-c27a-49d8-87dc-cd6413c120a2, of entity: Student
use single_table inheritance. doc for hibernate: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/inheritance.html
精彩评论