How can I select a boolean literal in HQL?

3.2k Views Asked by At

I can select int and string literals in HQL like so:

select 'a string' as StringLiteral, 1 as IntLiteral from Eg.MyClass

but I haven't found a way to select a boolean literal. I've tried select true ..., and select new bool true ... but those cause HQL syntax exceptions.

Is there a way to select a boolean literal in HQL?

I have found a workaround (select case when (1=1) then true...), but that seems inefficient...

2

There are 2 best solutions below

1
On BEST ANSWER

There are actually no real booleans in SQL (as in Select 1=1), except of in conditions. HQL turns "true" and "false" into 0 and 1 I think (something you can configure somewhere), but I guess it is still an integer.

Simplest things you can most probably do is convert the integer to a boolean in memory after the query.

1
On
select cast(1 as bool) as bitLiteral from Eg.MyClass

NHibernate usually supports the same features as Hibernate. This was elaborated from this answer. I have tested it with NHibernate.

NHibernate maps SQL bit type to .Net bool type.

The cast is required in plain SQL too, as SQL (at least SQL Server Transact-SQL) lacks bit literals.