Movatterモバイル変換


[0]ホーム

URL:


ObjectDBObjectDB
ObjectDB Manual

Numbers in JPQL and Criteria Queries

Numeric values may appear in JPQL queries in many forms:

This page covers the following topics:

Arithmetic OperatorsThe ABS FunctionThe MOD FunctionThe SQRT FunctionCriteria Query Arithmetic Expressions

Arithmetic Operators

The following arithmetic operators are supported by JPA:

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:

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:

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:

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.

< JPQL Paths and Types^ Query ExpressionsStrings in JPQL >

[8]ページ先頭

©2009-2025 Movatter.jp