Nhibernate查询API中相同表之间的内部联接

时间:2010-01-26 14:06:25

标签: nhibernate tsql

我有两个正确映射的域对象:电子邮件和ActionPlans

表结构类似于:

create table emails(id int identity(1,1), subject nvarchar(512), parentEmailId int, rootEmailId int)

create table actionPlans(id int identity(1,1), dueDate datetime, emailId int)

insert into emails(subject, parentEmailId, rootEmailId)
values('First email', null,1)

insert into emails(subject, parentEmailId, rootEmailId)
values('RE: FirstEmail', 1, 1)

insert into emails(subject, parentEmailId, rootEmailId)
values('RE: RE: FirstEmail', 2, 1)

insert into actionPlans(dueDate, emailId)
values('2011-01-01', 2)

parentEmailId是层次结构,rootEmailId始终是第一条消息(启动电子邮件)。 在层次结构的每个级别,我都可以创建一个ActionPlan(每个层次结构只有一个)

现在我使用rootEmailId

获取actionPlan

SomeThing赞:

 return
            this.CreateCriteria(typeof(ActionPlan), "ap")
            .CreateAlias("Message", "m")
            .Add(Restrictions.Eq("m.RootMessage.Id", rootMessageId))
            .UniqueResult<ActionPlan>();

它产生类似的东西:

select a.* FROM actionPlans a
inner Join emails e on a.emailId = e.id
where e.rootEmailId = 1

工作正常:但是这让我很头疼,因为我需要从db获取电子邮件并获取rootEmailid

现在我想发送emailId而不是rootEmailId 那么如何在Nhibernare查询API中翻译以下TSQL?

select a.*
from actionPlans a
    inner join emails rootEmail
        on rootEmail.id = a.emailId
    inner join emails e
        on e.rootEmailId = rootEmail.rootEmailId
where e.id = 2

现在我可以发送2,1或3并获得相同的ActionPlan

由于

0 个答案:

没有答案