I'm trying to write an HQL query which will calculate an average rating for an item. I want the query to return 0 instead of null when there are no rating for a given item - so that I can use my query as a subquery. So is it possible? Is there an HQL equivalent of IFNULL or NVL?
HQL or Hibernate Query Language is the object-oriented query language of Hibernate Framework.
No. You have to use is null and is not null in HQL. Save this answer.
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 .
COALESCE is the official equivalent.
It returns the first non-null of its arguments.
Example:
COALESCE(id_pati, 0)
Link Wikipedia
Nhibernate docs are out of date. Check http://docs.jboss.org/hibernate/stable/core/reference/en/html/queryhql.html
If nothing works you can try:
select
case
when something is not NULL then 0
else
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With