Numbers in JPQL and Criteria Queries
Numeric values may appear in JPQL queries in many forms:
- as numeric literals - e.g.
123,-12.5. - asparameters- when numeric values are assigned as arguments.
- as path expressions - in navigation to persistent numeric fields.
- asaggregate expressions - e.g. COUNT.
- ascollection functions - when the return value is numeric, e.g. INDEX, SIZE.
- asstring functions - when the return value is numeric, e.g. LOCATE, LENGTH.
- as composite arithmetic expressions that use operators and functions to combine simple numeric values into a more complex expression.
This page covers the following topics:
Arithmetic OperatorsThe ABS FunctionThe MOD FunctionThe SQRT FunctionCriteria Query Arithmetic ExpressionsArithmetic 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 anint value and adouble value isdouble.
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 to5ABS(10.7)is evaluated to10.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 to2 (3 goes into 11 three times with a remainder of 2)MOD(8, 4)is evaluated to0 (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. forint andlong operands the MOD function returns along value).
The SQRT Function
The SQRT function returns the square root of a specified argument.
For example:
SQRT(9)is evaluated to3SQRT(2)is evaluated to1.414213562373095
The SQRT function takes as an argument a numeric value of any type and always returns adouble value.
Criteria Query Arithmetic Expressions
JPQL arithmetic operators and functions (which are described above) are available also as JPA criteria query expressions. TheCriteriaBuilderjakarta.persistence.criteria.CriteriaBuilder - JPA Interface Used to construct criteria queries, compound selections, expressions, predicates, orderings. 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:Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<Integer> path= country.getPath .get(attributeName)-JPAMethodCreate a path correspondingto the referenced attribute.("population");Expressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> param= cb.parameterCriteriaBuilder.parameter(paramClass)-JPAMethodCreate a parameter expression.(Integer.class);// Addition (+)Expressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> sum1= cb.sumCriteriaBuilder.sum(x,y)-JPAMethodCreate an expression that returns the sum of its arguments.(path, param);// 2 expressionsExpressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> sum2= cb.sumCriteriaBuilder.sum(x,y)-JPAMethodCreate an expression that returns the sum of its arguments.(path,1000);// expression + numberExpressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> sum3= cb.sumCriteriaBuilder.sum(x,y)-JPAMethodCreate an expression that returns the sum of its arguments.(1000, path);// number + expression// Subtraction (-)Expressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> diff1= cb.diffCriteriaBuilder.diff(x,y)-JPAMethodCreate an expression that returns the difference between its arguments.(path, param);// 2 expressionsExpressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> diff2= cb.diffCriteriaBuilder.diff(x,y)-JPAMethodCreate an expression that returns the difference between its arguments.(path,1000);// expression - numberExpressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> diff3= cb.diffCriteriaBuilder.diff(x,y)-JPAMethodCreate an expression that returns the difference between its arguments.(1000, path);// number - expression// Multiplication (*)Expressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> prod1= cb.prodCriteriaBuilder.prod(x,y)-JPAMethodCreate an expression that returns the product of its arguments.(path, param);// 2 expressionsExpressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> prod2= cb.prodCriteriaBuilder.prod(x,y)-JPAMethodCreate an expression that returns the product of its arguments.(path,1000);// expression * numberExpressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> prod3= cb.prodCriteriaBuilder.prod(x,y)-JPAMethodCreate an expression that returns the product of its arguments.(1000, path);// number * expression// Division (/)Expressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> quot1= cb.quotCriteriaBuilder.quot(x,y)-JPAMethodCreate an expression that returns the quotient of its arguments.(path, param);// 2 expressionsExpressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> quot2= cb.quotCriteriaBuilder.quot(x,y)-JPAMethodCreate an expression that returns the quotient of its arguments.(path,1000);// expression / numberExpressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> quot3= cb.quotCriteriaBuilder.quot(x,y)-JPAMethodCreate an expression that returns the quotient of its arguments.(1000, path);// number / expression// Modulo (%)Expressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> mod1= cb.modCriteriaBuilder.mod(x,y)-JPAMethodCreate an expression that returns the modulus (remainder under integer division) of its arguments.(path, param);// 2 expressionsExpressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> mod2= cb.modCriteriaBuilder.mod(x,y)-JPAMethodCreate an expression that returns the modulus (remainder under integer division) of its arguments.(path,1000);// expression % numberExpressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> mod3= cb.modCriteriaBuilder.mod(x,y)-JPAMethodCreate an expression that returns the modulus (remainder under integer division) of its arguments.(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:
Expressionjakarta.persistence.criteria.Expression-JPAInterfaceTypefor query expressions.<Integer> abs= cb.absCriteriaBuilder.abs(x)-JPAMethodCreate an expression that returns the absolute value of its argument.(param);// ABS(expression)Expressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> neg= cb.negCriteriaBuilder.neg(x)-JPAMethodCreate an expression that returns the arithmetic negation of its argument.(path);// -expressionExpressionjakarta.persistence.criteria.Expression -JPAInterfaceTypefor query expressions.<Integer> sqrt= cb.sqrtCriteriaBuilder.sqrt(x)-JPAMethodCreate an expression that returns the square root of its argument.(cb.literalCriteriaBuilder.literal(value)-JPAMethodCreate an expressionfor a literal.(100));// SQRT(expression)
As shown above, a number can always be converted to a numeric expression by using theliteralCriteriaBuilder.literal(value) - JPA Method Create an expression for a literal. method.