NHibernate - 映射到辅助键的最佳方式

发布于 2024-09-13 09:38:35 字数 996 浏览 2 评论 0原文

我正在尝试将新的域模型映射到固定模式遗留数据库,但一直困惑于如何进行某种类型的映射。这是我的问题的摘要。

工人有多种类型。例如,HourlyWorker 和 SalariedWorker。

HourlyWorker 的数据存储在 WORKERS 表和 HOURLY_WORKERS 表中,两者之间具有标准外键关系 WORKERS.PK_WORKERS = HOURLY_WORKERS.FK_WORKERS。

从 NHibernate 的角度来看,HourlyWorker 的“Id”被映射到 WORKERS.PK_WORKERS,这是应该的。

到目前为止,一切都很好。

现在假设有一个对象仅适用于小时工。

class Timesheet
  HourlyWorker Owner { get; private set; }

在旧数据库中,它以一种相当简单的方式映射到 TIMESHEETS 表,但有一个问题:到所有者的外键链接不是通过到 WORKER 表的链接来表达的,而是通过到 HOURLY_WORKER 表的链接来表达的。换句话说,外键 FK_HOURLY_WORKER 是一个唯一标识符,但指向 NHibernate 视图中主键的值。

我不能简单地将 Id HourlyWorker 更改为映射到 HOURLY_WORKER 表,因为 70% 引用小时工的数据库使用 WORKER 中的主键作为外键 Id。

映射此关系的选项有哪些?

可能会使此过程变得更容易:所有者属性是只读的 - 时间表无法更改其所有者。但是,当删除 HourlyWorker 时,删除应级联到其所有时间表。

最后,请不要回复建议我更改数据库。我知道。如果您想要我客户的电话号码,以便您可以尝试说服他们,他们的架构并不是现有的最完美的东西,那么我可以提供。我已经为这场斗争奋斗了几个月。另外,请不要回应说这表明问题是管理问题而不仅仅是 NHibernate 映射问题。我也知道这一点。目前,我只想获得有关如何解决技术问题的建议。

I am trying to map a new domain model to a fixed-schema legacy database and am stuck on how to do a certain type of mapping. Here's a distilation of my problem.

There are types of Workers. For example an HourlyWorker and a SalariedWorker.

The data for an HourlyWorker is stored in the WORKERS table and the HOURLY_WORKERS table with a standard foreign key relation between the two of WORKERS.PK_WORKERS = HOURLY_WORKERS.FK_WORKERS.

The "Id" of the HourlyWorker from NHibernate's point of view is mapped to WORKERS.PK_WORKERS, which is as it should be.

So far so good.

Now suppose there is an object that applies only to hourly workers.

class Timesheet
  HourlyWorker Owner { get; private set; }

In the legacy database this maps to the TIMESHEETS table in a fairly straightforward manner but with one catch: The foreign-key link to the owner is expressed not through a link to the WORKER table but via a link to the HOURLY_WORKER table. In other words, the foreign key FK_HOURLY_WORKER is a unique identifier but is not pointing at the value that is in NHibernate's view the primary key.

I cannot simply change the Id HourlyWorker to map to the HOURLY_WORKER table because 70% of the database referencing houry workers uses the primary key from WORKER as the foreign key Id.

What are my options for mapping this relationship?

Something that might make this easier: the Owner property is read only - the Timesheet cannot have it's owner changed. However, when an HourlyWorker is deleted, deletes should cascade to all of their Timesheets.

Finally, please don't respond with recommendations that I change the database. I know. If you want my client's phone number so that you can try to convince them that their schema is not the most perfect thing in existence then I can supply it. I have been fighting this fight for months. Also please don't respond that this indicates that the problem is managerial and not just NHibernate mappings. I know that too. For right now, I just want advice on how to solve the technical problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

糖果控 2024-09-20 09:38:35

我相信您正在寻找多对一元素的 property-ref 属性:
NHibernate 文档

顺便说一下,映射到旧版模式很难——做好承受严重痛苦的准备!从好的方面来说,当您走到另一边时,您将成为一名 NHibernate 专家。

I believe you are looking for the property-ref attribute of the many-to-one element:
NHibernate docs

By the way, mapping to a legacy schema is hard - be prepared for some serious pain! On the plus side, you'll be an NHibernate expert by the time you come out on the other side.

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