NHibernate 内连接主从除 id 以外的列 [英] NHibernate inner join master-detail on a column other than the id
问题描述
假设有以下映射条目:
<类名="客户"表="客户">
< class name="Customer" table="customer">
<id name="InternalId" column="uintCustomerId" type="long">
<generator class="identity" />
</id>
<!-- The BinaryBlob below is a 16 byte array - Think Guid.NewGuid().ToByteArray -->
<property name="CustomerUid" column="uidCustomerId" type="BinaryBlob" not-null="true" />
<property name="Name" column="strFullName" type="String" not-null="true" />
<property name="Age" column="intAge" type="System.Int32" not-null="true" />
<set name="Orders" table="order" generic="true" inverse="false" lazy="false" cascade="all">
<key column="uidCustomerId" />
<one-to-many class="Order" />
</set>
<类名="订单"表="订单">
< class name="Order" table="order">
<id name="InternalId" column="uintOrderId" type="long">
<generator class="identity" />
</id>
<!-- This BinaryBlob is a 16 byte array - Think Guid.NewGuid().ToByteArray -->
<property name="CustomerUid" column="uidCustomerId" type="BinaryBlob" not-null="true" />
<property name="OrderDate" column="datOrderDate" type="System.DateTime" not-null="true" />
因此有 2 个类:客户 - 具有上述映射中定义的属性的订单
So there are 2 classes: Customer - Order with properties as defined in the above mapping
客户 PK 是 uintCustomerId
The Customer PK is uintCustomerId
订单 PK 是 uintOrderId
The Order PK is uintOrderId
uidCustomerId 是 Customer 表上的唯一键,是 Order 表上 Customer 的 fkuidCustomerId 是二进制 (16)
uidCustomerId is a unique key on the Customer table and the fk to Customer on the Order table uidCustomerId is binary(16)
数据库无法更改.
鉴于上述情况.我们如何使用 NHibernate Criteria API 来查询给定日期之后的 Customer 及其相关订单.注意:我们需要加入 uidCustomerId 这不是主键.
Given the above. How can we query using the NHibernate Criteria API to bring a Customer and his related Orders that are after a given date. NOTE: We need to join on uidCustomerId which is not the primary key.
这是否可以通过 Criteria API 完成?这可以用 HQL 来完成吗?
Is this something that can be done with the Criteria API? Is this something that can be done with HQL?
谢谢,塔索斯
推荐答案
我已经尝试过与您要求的类似的东西.这是我混搭的代码(未经测试).
I've tried similar stuff as what you required. Here's the code that I mashed up (without testing).
IList customers = sess.CreateCriteria(typeof(Customer))
.CreateAlias("Orders", "order")
.CreateAlias("order.OrderDate", "orderdate")
.Add( Expression.Ge("orderdate", somedate) )
.List();
这里是 NH CreateAlias
这篇关于NHibernate 内连接主从除 id 以外的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!