2011-02-03 26 views
5

編輯:具體談論沒有表查詢。是的,我可以用存在,但我不得不這樣做如何選擇行中是否存在HQL

select case when exists (blah) then 1 else 0 end as conditionTrue 
from ARealTableReturningMultipleRows 

在T-SQL我可以這樣做:

select case when exists(blah) then 1 else 0 end as conditionTrue 

在Oracle我可以這樣做:

select case when exists(blah) then 1 else 0 end as conditionTrue from DUAL 

如何在HQL中實現同樣的功能?

select count()似乎是第二好的選擇,但如果我不需要,我不想處理表中的每一行。

+0

看看這個問題:http://stackoverflow.com/questions/3672444/where-exists-in-hibernate-hql – 2011-02-03 20:35:41

回答

1

簡短回答:我相信這是不可能的。

我的理由:

根據Where can I find a list of all HQL keywords? Hibernate項目並沒有公佈在其網站上HQL的語法,它可以在Hibernate全分佈雖然.g ANTLR文件。

我沒有與ANTLR .g文件太多的經驗,但你可以在文件中找到這個(hibernate-distribution-3.6.1.Final/project/core/src/main/antlr/hql.g):

selectFrom! 
     : (s:selectClause)? (f:fromClause)? {                          
       // If there was no FROM clause and this is a filter query, create a from clause. Otherwise, throw         
       // an exception because non-filter queries must have a FROM clause.                
       if (#f == null) {                             
         if (filter) {                            
           #f = #([FROM,"{filter-implied FROM}"]);                   
         }                               
         else                              
           throw new SemanticException("FROM expected (non-filter queries must contain a FROM clause)");      
       }                                 

其中明確規定有一些HQL查詢沒有FROM條款,但這是可以接受的,如果這是一個過濾器查詢。現在我不再是HQL/Hibernate的專家,但我相信過濾器查詢不是完整的查詢,而是您使用session.createFilter(請參閱How do I turn item ordering HQL into a filter query?)定義的內容,因此我認爲無法省略FROM子句。

0
+0

Editted的問題,以更好地反映了我的意思。抱歉讓你困惑。 – 2011-02-03 20:58:35

+0

啊,我看,看起來和我在很久以前問過的一個沒有答案的問題非常相似。如果你得到一個有效的,也許你會考慮將其添加到我的問題。 http://stackoverflow.com/questions/808543/select-constant-in-hibernate – digitaljoel 2011-02-03 21:59:45

1

我用一行假表與MyDual例如。

select case when exists(blah) then 1 else 0 end as conditionTrue from MyDual