我有一个需要使用 NHibernate 的 ICriteria API 表示的 SQL 查询。
SELECT u.Id as Id,
u.Login as Login,
u.FirstName as FirstName,
u.LastName as LastName,
gm.UserGroupId_FK as UserGroupId,
inner.Data1,
inner.Data2,
inner.Data3
FROM dbo.User u inner join
dbo.GroupMember gm on u.Id = gm.UserAnchorId_FK
left join
(
SELECT
di.UserAnchorId_FK,
sum(di.Data1) as Data1,
sum(di.Data2) as Data2,
sum(di.Data3) as Data3
FROM
dbo.DailyInfo di
WHERE di.Date between '2009-04-01' and '2009-06-01'
GROUP BY di.UserAnchorId_FK
) inner ON inner.UserAnchorId_FK = u.Id
WHERE gm.UserGroupId_FK = 195
迄今为止的尝试包括映射“用户”和“每日信息”类(我的实体)并将 DailyInfo 对象作为用户对象的属性。但是,如何映射它们之间的外键关系仍然是一个谜,即
<one-to-one></one-to-one>
<one-to-many></one-to-many>
<generator class="foreign"><param name="property">Id</param></generator> (!)
Web 上的解决方案通常与 WHERE 子句中的子查询有关,但是我需要在此子查询上保留连接,以确保为未连接的行返回 NULL 值。
我觉得我应该对外部查询使用 Criteria,然后用 DetachedCriteria 形成一个“join”来表示子查询?