У меня есть два сопоставленных справа доменных объекта: электронные письма и 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 (только один на всю иерархию)
Теперь я получаю actionPlan с использованием rootEmailId
SomeThing Like:
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
Так как же перевести следующий TSQL в API запросов Nhibernare?
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
Спасибо