Mathematical Operators in Criteria Queries

Given the mapped sleep class:

@Entity
public class MyTestClass {
  /* id and stuff */

  private Integer aValue;
  private Integer bValue;
}

You can do the following using HQL:

Query query 
  = getCurrentSession().createQuery("select aValue * bValue from MyTestClass");
List<Double> resultList = query.list;

and get the calculated result.

Is it possible to do something similar with the Criteria API? I still haven't found a way to use math operations with the Criteria API. We have aggregate functions like sum, avg, etc., but not basic math operators?

+3
source share
2 answers

You can create a new property in your class that is this computed value. Just specify the formula attribute for this property. You can then include this property in your criteria.

<property name="product" formula="aValue*bValue" />

formula (): SQL, . .

+5

sql. , - sqlProjection/sqlRestriction

+1

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


All Articles