当两个子类都可以实现时的 nhibernate 子类映射
我有以下课程
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; }
}
从概念上讲,同一个人可以既是员工又是客户。人员也可以在没有员工或客户记录的情况下存在。使用每个子类的表我怎样才能让它工作。
就目前情况而言,我看不到让 NHibernate 以这种方式工作的方法。如果我创建一个 Person,然后尝试使用现有的 Person Id 创建一个 Employee,NHibernate 仍会尝试插入到 Person 中。有没有办法让 NHibernate 意识到我已经有一个 Person 并且只想添加 Employee 记录?
如果可能的话,我宁愿不去“每个类表”或“每个层次结构表”。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的模型不正确。如果一个人可以既是客户又是员工,那么你不应该使用继承(一个员工是一个人),而应该使用组合(一个员工有一个[对应的]人或一个人有- 员工[角色])
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])
这在面向对象的实践中行不通。您可能希望遵循一条路线,表明一个人既具有客户的角色,又具有员工的角色。
下面是一个简单的例子,说明了为什么这在一般的 OO 术语中不起作用。
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.