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 shou开发者_JAVA技巧ld 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.
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.
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>();
精彩评论