How to choose if a string exists in HQL

EDIT: In particular, this is a query without a table. Yes, I can use, but I have to do

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

In T-SQL, I can do:

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

In Oracle, I can do:

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

How can I achieve the same in HQL?

select count () seems like a second alternative, but I don't want to process every row in the table if I don't need it.

+5
source share
3 answers

Short answer: I think this is NOT possible.

My reasoning is:

According to Where can I find a list of all HQL keywords? The Hibernate project does not publish HQL grammar on its website; it is available in the full distribution of Hibernate as a .gANTLR file.

.g ANTLR, (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 (. HQL ?), , FROM.

+1

, MyDual.

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

According to http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-expressions , it looks like they support both case and exist arguments.

0
source

All Articles