NHibernate - 将基于不同属性的集合映射到标识
考虑下面的表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 nhibernate 还很陌生,但这听起来像是我在使用棕地数据库时遇到的问题。 property-ref 属性允许引用主键以外的列。所以在你的情况下应该是:
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: