当两个子类都可以实现时的 nhibernate 子类映射

发布于 2024-10-08 17:09:32 字数 569 浏览 2 评论 0原文

我有以下课程

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ双果 2024-10-15 17:09:32

你的模型不正确。如果一个人可以是客户又是员工,那么你不应该使用继承(一个员工是一个人),而应该使用组合(一个员工有一个[对应的]人或一个人有- 员工[角色])

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])

生死何惧 2024-10-15 17:09:32

这在面向对象的实践中行不通。您可能希望遵循一条路线,表明一个人既具有客户的角色,又具有员工的角色。

下面是一个简单的例子,说明了为什么这在一般的 OO 术语中不起作用。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文