700 words

Numbers in JPQL and Criteria Queries

Numeric values may appear in JPQL queries in many forms:

Arithmetic Operators

The following arithmetic operators are supported by JPA:

  • 2 unary operators:  + (plus) and - (minus).
  • 4 binary operators:  + (addition), - (subtraction), * (multiplication) and / (division).

ObjectDB also supports the % (modulo) and the ~ (bitwise complement) operators that are supported in Java and JDO. JPA follows Java numeric promotion principles. For example, the resulting type of a binary arithmetic operation on an int value and a double value is double.

The ABS Function

The ABS function removes the minus sign from a specified argument and returns the absolute value, which is always a positive number or zero.

For example:

  • ABS(-5) is evaluated to 5
  • ABS(10.7) is evaluated to 10.7

The ABS function takes as an argument a numeric value of any type and returns a value of the same type.

The MOD Function

The MOD function calculates the remainder of the division of one number by another, similar to the modulo operator (%) in Java (which is also supported by ObjectDB as an extension).

For example:

  • MOD(11, 3) is evaluated to 2 (3 goes into 11 three times with a remainder of 2)
  • MOD(8, 4) is evaluated to 0 (4 goes into 8 twice with a remainder of 0)

The MOD function takes two integer values of any type and returns an integer value. If the two operands share exactly the same type the result type is the same. If the two operands have different types, numeric promotion is used as with binary arithmetic operations in Java (e.g. for int and long operands the MOD function returns a long value).

The SQRT Function

The SQRT function returns the square root of a specified argument.

For example:

  • SQRT(9) is evaluated to 3
  • SQRT(2) is evaluated to 1.414213562373095

The SQRT function takes as an argument a numeric value of any type and always returns a double value.

Criteria Query Arithmetic Expressions

JPQL arithmetic operators and functions (which are described above) are available also as JPA criteria query expressions. The CriteriaBuilderjavax.persistence.criteria.CriteriaBuilderJPA interfaceUsed to construct criteria queries, compound selections, expressions, predicates, orderings.
See JavaDoc Reference Page...
interface provides factory methods for building these expressions, as shown in the following examples.

Binary Operators

The creation of a binary arithmetic operator requires two operands. At least one operand must be a criteria numeric expression. The other operand may be either another numeric expression or a simple Java numeric object:

  // Create path and parameter expressions:
  Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> path = country.getget(attributeName)Path's methodCreate a path corresponding to the referenced attribute.
See JavaDoc Reference Page...
("population"); Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> param = cb.parameterparameter(paramClass)CriteriaBuilder's methodCreate a parameter expression.
See JavaDoc Reference Page...
(Integer.class);   // Addition (+) Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> sum1 = cb.sumsum(x, y)CriteriaBuilder's methodCreate an expression that returns the sum of its arguments.
See JavaDoc Reference Page...
(path, param); // expression + expression Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> sum2 = cb.sumsum(x, y)CriteriaBuilder's methodCreate an expression that returns the sum of its arguments.
See JavaDoc Reference Page...
(path, 1000); // expression + number Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> sum3 = cb.sumsum(x, y)CriteriaBuilder's methodCreate an expression that returns the sum of its arguments.
See JavaDoc Reference Page...
(1000, path); // number + expression   // Subtraction (-) Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> diff1 = cb.diffdiff(x, y)CriteriaBuilder's methodCreate an expression that returns the difference between its arguments.
See JavaDoc Reference Page...
(path, param); // expression - expression Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> diff2 = cb.diffdiff(x, y)CriteriaBuilder's methodCreate an expression that returns the difference between its arguments.
See JavaDoc Reference Page...
(path, 1000); // expression - number Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> diff3 = cb.diffdiff(x, y)CriteriaBuilder's methodCreate an expression that returns the difference between its arguments.
See JavaDoc Reference Page...
(1000, path); // number - expression   // Multiplication (*) Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> prod1 = cb.prodprod(x, y)CriteriaBuilder's methodCreate an expression that returns the product of its arguments.
See JavaDoc Reference Page...
(path, param); // expression * expression Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> prod2 = cb.prodprod(x, y)CriteriaBuilder's methodCreate an expression that returns the product of its arguments.
See JavaDoc Reference Page...
(path, 1000); // expression * number Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> prod3 = cb.prodprod(x, y)CriteriaBuilder's methodCreate an expression that returns the product of its arguments.
See JavaDoc Reference Page...
(1000, path); // number * expression   // Division (/) Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> quot1 = cb.quotquot(x, y)CriteriaBuilder's methodCreate an expression that returns the quotient of its arguments.
See JavaDoc Reference Page...
(path, param); // expression / expression Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> quot2 = cb.quotquot(x, y)CriteriaBuilder's methodCreate an expression that returns the quotient of its arguments.
See JavaDoc Reference Page...
(path, 1000); // expression / number Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> quot3 = cb.quotquot(x, y)CriteriaBuilder's methodCreate an expression that returns the quotient of its arguments.
See JavaDoc Reference Page...
(1000, path); // number / expression   // Modulo (%) Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> mod1 = cb.modmod(x, y)CriteriaBuilder's methodCreate an expression that returns the modulus of its arguments.
See JavaDoc Reference Page...
(path, param); // expression % expression Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> mod2 = cb.modmod(x, y)CriteriaBuilder's methodCreate an expression that returns the modulus of its arguments.
See JavaDoc Reference Page...
(path, 1000); // expression % number Expressionjavax.persistence.criteria.ExpressionJPA interfaceType for query expressions.
See JavaDoc Reference Page...
<Integer> mod3 = cb.modmod(x, y)CriteriaBuilder's methodCreate an expression that returns the modulus of its arguments.
See JavaDoc Reference Page...
(1000, path); // number % expression

The above examples demonstrate only integer expressions, but all the numeric types (byte, short, int, long, float, double, BigInteger, BigDecimal) are supported.

Unary Operators

Creation of the unary minus (-) operator and the ABS and SQRT functions requires one operand, which must be a numeric expression:

As shown above, a number can always be converted to a numeric expression by using the literalliteral(value)CriteriaBuilder's methodCreate an expression for a literal.
See JavaDoc Reference Page...
method.


This documentation explains how to use JPA in the context of the ObjectDB Object Database but mostly relevant
also for ORM JPA implementations, such as Hibernate (and HQL), EclipseLink, TopLink, OpenJPA and DataNucleus.
ObjectDB is not an ORM JPA implementation but an Object Database (ODBMS) for Java with built in JPA 2 support.