开发者

nhibernate subclass mapping when both subclasses can be materialized

I have the following classes

public class Person 
{
     public virtual int Id { get; set; }
}
public class Employee : Person
{
     public virtual int EmployeeProperty { get; set; }
}
public class Customer : Person
{
     public virtual int CustomerProperty { get; set; }
}

Conceptually a the same Person can 开发者_高级运维be both an Employee and a Customer. Also a Person can exist without either an Employee or Customer record. Using Table Per Subclass how can I get this to work.

As it stands now I don't see a way to get NHibernate to work this way. If I create a Person, and then try to create an Employee using an existing Person Id, NHibernate still tries to insert into Person. Is there a way to get NHibernate to realize that I already have a Person and just want to add the Employee record?

I would prefer to not go to Table Per Class or Table Per Hierarchy if possible.


You model is not correct. If a Person can be both a Customer and an Employee, then you should not use inheritance (An Employee is-a Person), but composition (An Employee has-a [corresponding] Person or A Person has-a Employee [role])


This doesn't work in practice in OO. You would probably want to follow a route that says a person has the roles of being a Customer and also has the role of being an Employee.

Here is a simple example of why this can't work in general OO terms.

Person a = new Employee();
Customer b = (Customer)a; // exception
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜