我目前正在嘗試從手工製作的hql轉移到通過DetachedCriteria構建的查詢。我有和HQL:在nHibernate中使用DetachedCriteria的複雜查詢
from GenericObject genericObject
left join fetch genericObject.Positions positions
where (positions.Key.TrackedSourceID, positions.Key.PositionTimestamp) in
(select gp.Key.TrackedSourceID, max(gp.Key.PositionTimestamp)
from GenericPosition gp
group by gp.Key.TrackedSourceID)
現在使用的DetachedCriteria:
var subquery = DetachedCriteria
.For (typeof (GenericPosition), "gp")
.SetProjection (Projections.ProjectionList()
.Add (Projections.Property ("gp.Key.TrackedSourceID"))
.Add (Projections.Max ("gp.Key.PositionTimestamp"))
.Add (Projections.GroupProperty ("gp.Key.TrackedSourceID"))
);
var criteriaQuery = DetachedCriteria
.For (typeof (GenericObject), "genericObject")
.CreateAlias ("genericObject.Positions", "positions")
.SetFetchMode ("genericObject.Positions", FetchMode.Eager)
.Add (Subqueries.In (??, subquery))
我不知道是什麼,而不是類型的?以創建表達式(positions.Key.TrackedSourceID,positions.Key.PositionTimestamp)
嗯,你是對的。但是,我正在動態創建此查詢,並調用方法將某些內容附加到查詢中。查詢結束後,我需要爲參數添加值,而且它非常不雅 - DetachedCriteria在這裏看起來更好。 – paszczi 2009-02-13 12:28:06
你是對的,我希望你的問題會有答案,...我發現自己在類似的情況... – 2010-02-26 07:46:48