请原谅我的业余冬眠,但我在下面的场景中抓取时很挣扎。
var query = session.CreateCriteria<Notification>().SetFetchMode("Parameters", FetchMode.Select)
.CreateAlias("Parameters", "p", JoinType.InnerJoin)
.Add(Restrictions.Where<Notification>(x => x.Acknowledged == false));
[一些构建名为npHashes的IList的代码]
query = query.Add(Restrictions.In("p.PairHash", npHashes)).AddOrder(new Order("DateCreated", false));
(枚举)
请注意,我使用SELECT作为预取模式…QueryOver中遗漏的一个选项…和LINQ……还请注意,所获取的表与我连接到的表是相同的。
执行该查询的结果如下:
SELECT
this_.Id as Id14_1_,
this_.Version as Version14_1_,
this_.Url as Url14_1_,
this_.DispatchType as Dispatch5_14_1_,
this_.Acknowledged as Acknowle6_14_1_,
this_.DateCreated as DateCrea7_14_1_,
this_.NotificationType as Notifica2_14_1_,
p1_.Id as Id15_0_,
p1_.Version as Version15_0_,
p1_.NotificationId as Notifica3_15_0_,
p1_.Name as Name15_0_,
p1_.Value as Value15_0_,
p1_.PairHash as PairHash15_0_
FROM
Notification this_
inner join
NotificationParameter p1_
on this_.Id=p1_.NotificationId
WHERE
this_.Acknowledged = ?p0
and p1_.PairHash in (
?p1
)
ORDER BY
this_.DateCreated desc;
?p0 = False [Type: Boolean (0)],
?p1 = 'V3zmXnv12B3AC26xeG10w+bas4U=' [Type: String (28)]
所以第一个问题是出于某种原因NotificationParameter列被包含在选择列表中…它似乎没有做select fetch。这是不好的,因为a)我想要一个选择获取b)获取记录被过滤。抓取不同于连接(作为一个概念),连接数据上的过滤器不应该(在这种情况下)过滤我获取的内容。
第二个问题当然是SELECT取回没有发生。相反,在第一次访问Notification的Parameters属性时,它们被惰性加载:O
帮忙吗?此外,如果有一种方法来做到这一点使用QueryOver我更喜欢。我注意到我可以去。underlyingcriteria . setfetchmode(....)但是这对获取的内容没有影响
在SQL中,您不能同时过滤和获取所有数据。我对查询还不是很熟悉,但是你应该有个概念。
var subquery = DetachedCriteria.For<Notification>()
.CreateAlias("Parameters", "p", JoinType.InnerJoin)
.Add(Restrictions.Where<Notification>(x => x.Acknowledged == false))
.Add(Restrictions.In("p.PairHash", npHashes))
.SetProjection(Projections.Id());
session.CreateCriteria<Notification>()
.Add(Subqueries.PropertyIn("Id", subquery))
.SetFetchMode("Parameters", FetchMode.Eager)
.AddOrder(Order.Asc("DateCreated"))
.List<Notification>();