Q:

HQL comparison inside the select statement

Consider the following query in HQL:
SELECT m.id, (SELECT (COUNT(*) > 0) FROM SubqueryTable...) FROM MyTable AS m;

I need to return information as boolean from the select query inside the select with information whether there are any entries that satisfy that query.
The above query would work fine translated in SQL, but hibernate gives me an error. I can't use comparison operators with an aggregate function.
What is the proper way to get the information from the subselect inside the select clause, in HQL?

A:

Not sure how this can be transformed into HQL but your query could be written as:
SELECT m.id,
       (SELECT COUNT(*) FROM SubqueryTable...) > 0
         AS b
FROM MyTable AS m;

But perhaps you could use this - which seems to be bypassing the problem as it is using INT columns:
SELECT m.id,
       CASE WHEN (SELECT COUNT(*) FROM SubqueryTable...) > 0
           THEN 1
           ELSE 0
       END AS b
FROM MyTable AS m;

You could also try with EXISTS:
SELECT m.id,
       EXISTS (SELECT * FROM SubqueryTable...) 
         AS b
FROM MyTable AS m;

or:
SELECT m.id,
       CASE WHEN EXISTS (SELECT * FROM SubqueryTable...) 
           THEN 1
           ELSE 0
       END AS b
FROM MyTable AS m;

