2010-03-12 72 views
5

我有一個Hibernate HQL問題。 我想寫一個子查詢作爲派生表(出於性能原因)。 在HQL中可以這樣做嗎? 例子:在Hibernate中使用派生表的子查詢HQL

FROM Customer WHERE country.id in 
(SELECT id FROM (SELECT id FROM Country where type='GREEN') derivedTable) 

(順便說一句,這只是一個簡單的查詢,所以不要重寫它給意見,只是派生表的概念我很感興趣)

回答

2

不幸的是沒有,派生表目前不在HQL中工作。 例如,下面的工作:

List<int> result = 
    nHSession.CreateQuery(@"select distinct Id from User u") 
    .List<int>().ToList(); 

...以下拋出此異常: 類型的異常 'Antlr.Runtime.NoViableAltException' 被拋出。近線1, 柱24

List<int> result = nHSession.CreateQuery(
    @"select distinct Id from (select u from User u)") 
    .List<int>().ToList(); 

的回落將創建包含原始SQL或命名查詢[從選擇不同ID(來自S2.BP.Model.User U選擇U)]創建一個存儲過程,並通過命名查詢調用它,就像這樣:

List<int> result = nHSession.GetNamedQuery("spUserIds") 
    .SetInt32("id", 3) 
    .List<int>().ToList();