Bitwise And With NHibernate And Oracle

I am using Fluent NHibernate 1.0RC (for NHibernate 2.1.4000) along with Linq 2 NHibernate, and I want to execute the request with bitwise and operational. At first I tried to use Linq, but this did not work:

var objects = _session.Linq<MyClass>() .Where(x => (x.someInteger & otherInteger) > 0) .ToList(); 

My conclusion was that bitwise operations were not supported by Linq 2 Nhibermate. So I tried using HQL:

 var objects = _session.CreateQuery("select c from MyClass c where c.someInteger & :param > 0") .SetParameter("param", otherInteger) .List<MyClass>(); 

That didn't work either. This gave me error ora-01036: "illegal variable name / number".

So my questions are: is it possible to use bitwise operations with NHibernate? Is it supported out of the box with NHibernate 3.0? Is this problematic because I'm using Oracle DB, which will expect the bitand () function instead of the and operator?

+4
source share
1 answer

You can extend the Oracle dialect and add BITAND as a recognized HQL function:

 public class OraclePlusDialect : Oracle10gDialect { public OraclePlusDialect() { RegisterFunction("bitand", new StandardSQLFunction("bitand", NHibernateUtil.Int32)); } } 

Then you can fulfill your request as follows:

 var objects = _session.CreateQuery("select c from MyClass c where bitand(c.someInteger, :param) > 0") .SetParameter("param", otherInteger) .List<MyClass>(); 

Perhaps Oracle has a type conversion problem because BITAND returns a rarely used data type. If so, change your HQL query to:

 select c from MyClass c where bitand(c.someInteger, :param) + 0 > 0 
+2
source

Source: https://habr.com/ru/post/1339557/


All Articles