使用 SetFetchMode
请原谅我的业余休眠能力,但我在下面的场景中很难获取。
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 列包含在选择列表中...它似乎没有执行选择提取。这很糟糕,因为 a) 我想要选择提取 b) 提取记录被过滤。获取与连接(作为一个概念)不同,连接数据上的过滤器不应该(在这种情况下)过滤我获取的内容。
第二个问题当然是 SELECT 获取没有发生。相反,在第一次访问通知的参数属性时,它们会被延迟加载:O
有帮助吗?另外,如果有一种方法可以使用 QueryOver 来做到这一点,我会更喜欢。我注意到我可以去 .UnderlyingCriteria.SetFetchmode(....) 但这对获取的内容没有影响。
Excuse my amateur nhibernate-ness but I am struggling with fetching in the below scenario.
var query = session.CreateCriteria<Notification>().SetFetchMode("Parameters", FetchMode.Select)
.CreateAlias("Parameters", "p", JoinType.InnerJoin)
.Add(Restrictions.Where<Notification>(x => x.Acknowledged == false));
[some code that builds an IList called npHashes]
query = query.Add(Restrictions.In("p.PairHash", npHashes)).AddOrder(new Order("DateCreated", false));
[enumerate it]
Note that I am using SELECT as the prefetchmode... an option that apparently got left out of QueryOver... and LINQ... Also note the fetched table is the same table that i have joined on to to filter by.
Executing that query results in this:
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)]
So the first issue is for some reason NotificationParameter columns are included in the select list... it appears to not be doing a select fetch. This is bad because a) i want a select fetch b) the fetch records are filtered. Fetching is not the same as joining (as a concept) and the filters on the joined data should not (in this case) be filtering what I fetch.
Second issue of course is the SELECT fetch didn't happen. Instead on first accessing of the Parameters property of Notification they are lazily loaded :O
Any help? Also if theres a way to do this using QueryOver i'd prefer that. I noticed that I could go .UnderlyingCriteria.SetFetchmode(....) however that had no effect on what was fetched.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 sql 中,你不能同时过滤和获取所有内容。我对查询还不太熟悉,但你应该明白了。
in sql you cant filter and fetch all at the same time. I'm not that familiar with query over yet but you should get the idea.