Skip to content

Hql functions

gert-wijns edited this page Sep 13, 2014 · 2 revisions

Common hql functions are provided through query.hqlFunction().

Overview of the most notable functions:

  • Distinct
  • Count/CountDistinct
  • Numeric aggregate functions: min/max/avg/sum
  • Coalesce (hibernate supports this function, returns the first non-null argument)
PersonDto dto = query.select(PersonDto.class);
dto.setThePersonsName(query.hqlFunction().
    coalesce(person.getName()).or("Bert").select());

=> "select coalesce (hobj1.name,:np1) as thePersonsName 
    from Person hobj1"
params [np1="Bert"]
  • Cast, casts/converts the value to a different type. This makes use of the hibernate typehelper to decide which type name to cast to given the desired java type (only basic types are supported).

    Use case: a number is stored in a String property and needs to be used to build a restriction.
Product product = query.from(Product.class);
query.whereNumber(query.hqlFunction().
    cast(product.getManyProperties().getProperty1(), Long.class)).
    lt(10.0d);

=> "from Product hobj1 
    where cast(hobj1.manyProperties.property1 as long) < 10.0"
Clone this wiki locally