2

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...

brainbolt
  • 616
  • 1
  • 10
  • 24

2 Answers2

2
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.

Frédéric
  • 9,364
  • 3
  • 62
  • 112
  • Wow, this is really great. Didn't know this. The cool thing about it is that you can use the (N)Hibernate type after `as`, not some kind of DBMS specific type. You still cannot cast a boolean expression to bool (as in `1=1`, only when using a `CASE` expression), something I often miss. – Stefan Steinegger Mar 14 '16 at 07:33
1

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.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193