我有三个实体,配置为
Contact
HasOne(e => e.User).PropertyRef(e => e.Contact).Cascade.All();
User
HasMany(e => e.Requests);
References(e => e.Contact);
Request
References(e => e.User);
然后我这样查询:
CurrentSession.Query<Request>()
.Fetch(x => x.User)
.ThenFetch(x => x.Contact)
.ToList();
看起来Fetch工作正常,因为在第一次选择中我看到所有表格都已加入(为简洁起见省略了其他字段):
SELECT request0_.Id as Id128_0_,
user1_.Id as Id131_1_,
contact2_.Id as Id77_2_,
FROM requests request0_
left outer join users user1_
on request0_.user_id = user1_.Id
left outer join contacts contact2_
on user1_.contact_id = contact2_.Id
然后出于某种原因关注N + 1选项,我无法理解并且不知道如何解决问题:
SELECT user0_.Id as Id131_0_,
FROM users user0_
WHERE user0_.contact_id = 200 /* @p0 - contact_id */