NHibernate - 将基于不同属性的集合映射到标识

发布于 2024-11-30 18:51:43 字数 1434 浏览 1 评论 0原文

考虑下面的表:

Client(Id bigint, Name varchar(50))
Employee(Id bigint, Name varchar(50), ClientId bigint)

它们的映射如下:

<class name="Client" table="`Client`">
  <id name="Id" column="`Id`" type="long">
    <generator class="native" />
  </id>
  <property name="Name" column="`Name`" />
  <bag name="Employees" cascade="all" inverse="true" >
    <key column="`ClientId`" />
    <one-to-many class="Employee" />
  </bag>
</class>

<class name="Employee" table="`Employee`">
  <id name="Id" column="`Id`" type="long">
    <generator class="native" />
  </id>
  <property name="Name" column="`Name`" />
  <many-to-one name="Client" cascade="all" column="`ClientId`" />
</class>

如果我得到一个客户,我也会得到一个员工集合,其中 Employee.Client = Client.Id。伟大的。

现在考虑一下:

Client(Id bigint, Name varchar(50), AlternativeId int)
Employee(Id bigint, Name varchar(50), ClientId bigint, AlternativeClientId int)

我想返回一个包含 Employee 集合的 Client,其中 Employee.AlternativeClientId = Client.AlternativeId。

我假设关键节点现在应该是:

<key column="`AlternativeClientId`"/>

但除此之外我就很难了。有可以应用于集合的过滤器,但第二个版本中的员工集可能不是第一个版本中的员工子集,所以我认为这不是前进的方向。我尝试过,但这似乎是一个死胡同。是否有某种方法可以指定查询,但不仅限于具有 ClientId = Client.Id 的员工?

(对于“为什么”:这与不同的系统对数据有不同的看法有关。)

Consider the following tables:

Client(Id bigint, Name varchar(50))
Employee(Id bigint, Name varchar(50), ClientId bigint)

that are mapped like this:

<class name="Client" table="`Client`">
  <id name="Id" column="`Id`" type="long">
    <generator class="native" />
  </id>
  <property name="Name" column="`Name`" />
  <bag name="Employees" cascade="all" inverse="true" >
    <key column="`ClientId`" />
    <one-to-many class="Employee" />
  </bag>
</class>

<class name="Employee" table="`Employee`">
  <id name="Id" column="`Id`" type="long">
    <generator class="native" />
  </id>
  <property name="Name" column="`Name`" />
  <many-to-one name="Client" cascade="all" column="`ClientId`" />
</class>

If I get a Client, I also get a collection of Employees where the Employee.Client = Client.Id. Great.

Now consider this:

Client(Id bigint, Name varchar(50), AlternativeId int)
Employee(Id bigint, Name varchar(50), ClientId bigint, AlternativeClientId int)

I want to return a Client with a collection of Employees where Employee.AlternativeClientId = Client.AlternativeId.

I would assume the key node would now read:

<key column="`AlternativeClientId`"/>

But beyond that I'm stumped. There are filters that can be applied to collections, but the set of Employees in the second version may not be a subset of the Employees in the first version, so I don't think that is the way forward. I tried but that seemed to be a dead end. IS there some way of specifying a query, but not only on Employees that have the ClientId = Client.Id?

(For the 'whys': It's to do with how different systems have different views of the data.)

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

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

发布评论

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

评论(1

成熟的代价 2024-12-07 18:51:43

我对 nhibernate 还很陌生,但这听起来像是我在使用棕地数据库时遇到的问题。 property-ref 属性允许引用主键以外的列。所以在你的情况下应该是:

    <key column="AlternativeClientId" property-ref="AlternativeId"/>

I'm pretty new to nhibernate but this sounds like a problem I had due to working with a brownfield database. The property-ref attribute allows a column other than the primary key to be referenced. So in your case it should be:

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