2012-01-30 22 views
0

希望有人能回答這個問題。使用QueryOver而無子查詢的HQL大小

我知道我可以做以下使用HQL(僞下面的代碼)

var hql = "from objectA l where size(l.ChildCollection) > 0"; 

var data = Session.CreateQuery(hql) 
      .List<objectA>(); 

有沒有想知道如果你能做到使用QueryOver類似的東西。不訴諸使用子查詢。我在ChildCollection上有一個會話過濾器。

不幸的是,

var query = QueryOver.Of<ObjectA>() 
.WhereRestrictionOn(x => x.ChildCollection).IsNotEmpty(); 

主要生產,

WHERE 
    exists(
     select 
      1 
     from 
      [ChildCollection] 
     where 
      this_.Id=ObjectA_Id 
    ); 

凡爲HQL的產生,

where 
(
    select 
     count(childcollection1_.ObjectA_Id) 
    from 
     [ChildCollection] childcollection1_ 
    where 
     objectA0_.Id=childcollection1_.ObjectA_Id 
     and childcollection1_.DTCreated between @p0 and @p1 
    )>0 

乾杯

Tanzy

回答

0
var query = QueryOver.Of<ObjectA>().WhereRestrictionOn(x => x.ChildCollection).IsNotEmpty(); 

兩個查詢會產生類似於SQL:

SELECT this_.Id 
FROM [ObjectA] this_ 
WHERE exists(select 1 from [ChildObjectB] where this_.Id = [key]) 

難道這就是你想達到什麼目的?

+0

我已經編輯的問題,因爲不能適應評論信息 – Tanzy 2012-01-31 09:21:32

+0

你能解釋你爲什麼要生成*完全相同的SQL? 'sql-server'中的'執行計劃'對於這兩個查詢都是相同的 – MonkeyCoder 2012-01-31 09:52:44

+0

ChildCollection爲它分配了一個過濾器,所以當在HQL中使用size(ChildCollection)時,它包括已經在其限制上設置的會話過濾器。但是,使用QueryOver IsNotEmpty不會使用此會話篩選器。 – Tanzy 2012-01-31 13:25:47