Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate NVL in HQL

Tags:

hibernate

hql

People also ask

What is NVL in HQL?

NVL Function - PL/HQL Reference NVL function returns first non-NULL expression.

IS NULL check in HQL?

No. You have to use is null and is not null in HQL.

What is coalesce in HQL?

The equivalent to the nvl command in HQL is the coalesce command. coalesce(a,b) will return a if a is not null, otherwise b . So you would want something on the lines of: from Table where col1 = coalesce(:par1, 'asdf')

Is NVL better than coalesce?

NVL and COALESCE are used to achieve the same functionality of providing a default value in case the column returns a NULL. The differences are: NVL accepts only 2 arguments whereas COALESCE can take multiple arguments. NVL evaluates both the arguments and COALESCE stops at first occurrence of a non-Null value.


The equivalent to the nvl command in HQL is the coalesce command. coalesce(a,b) will return a if a is not null, otherwise b.

So you would want something on the lines of:

from Table where col1 = coalesce(:par1, 'asdf')

If your underlying database is Oracle then you can use the nvl function, I tried it and it worked for me.

Query query = session.createQuery(
                    " select ft from FeatureToggle ft left outer join ft.featureToggleCustomerMap "
                    + " ftcm where nvl(ftcm.custId,:custId) = :custId");

query.setParameter("custId", Long.valueOf(custId));

Your use case can be diffrent and you can use the nvl function as per your requirement if the database is nvl, not sure about the implecation of the other databases, as I have used this code only for Oracle. Hope it helps.