tags:

views:

788

answers:

3

In HQL, how can I use bitwise operators? I want the resulting SQL query to look something like

SELECT RoleId, RoleName, RolePerms WHERE (RolePerms & @Parameter) = @Parameter

However, writing this HQL

select from Role where (RolePerms & :param) = :param

gives me this error: NHibernate.Hql.Ast.ANTLR.QuerySyntaxException: Exception of type 'Antlr.Runtime.NoViableAltException' was thrown.

A: 

From section 13.8 of the NHibernate documentation, HQL does not support these bitwise operators. You'd have to revert to native SQL (see section 15).

David M
That's what I thought too until I found a test (http://bit.ly/1Wjk1) while going through NHibernate's source. According to the test, it seems to be possible.
Husain
In that case it looks like this might be something that has been fixed in 2.1, or will be fixed in a subsequent release. What version are you using?
David M
I am using version 2.1
Husain
A: 

According to the NHibernate Jira it looks like this should be working as of 2.1.0Beta2, http://nhjira.koah.net/browse/NH-1192.

Andrew Hanson
A: 

I found a solution to this. Writing the HQL this way works:

select r from Role r where (r.Permissions & :param) > 0
Husain