Logical Operators in JPQL and Criteria API
Logical operators in JPQL and in JPA criteria queries enable composition of complex JPQL boolean expressions out of simple JPQL boolean expressions.
This page covers the following topics:
Logical OperatorsBinary AND (&&) OperatorBinary OR (||) OperatorUnary NOT (!) OperatorCriteria Query Logical OperatorsLogical Operators
ObjectDB supports 2 sets of logical operators, as shown in the following table:
| Set 1 - JPQL / SQL | Set 2 - Java / JDO |
|---|---|
| AND | && |
| OR | || |
| NOT | ! |
JPQL follows the SQL notation, while Java uses its own notation (which is also in use by JDOQL, the JDO Query Language). ObjectDB supports both forms.
Binary AND (&&) Operator
The following query retrieves countries whose population and area (both) exceed specified limits:
SELECT c FROM Country c WHERE c.population > :population AND c.area > :area
A valid operand of an AND operator must be one of: TRUE, FALSE, and NULL.
The following table shows how the AND operator is evaluated based on its two operands:
| TRUE | FALSE | NULL | |
|---|---|---|---|
| TRUE | TRUE | FALSE | NULL |
| FALSE | FALSE | FALSE | FALSE |
| NULL | NULL | FALSE | NULL |
NULL represents unknown. Therefore, if one operand is NULL and the other operand is FALSE the result is FALSE, because one FALSE operand is sufficient for a FALSE result. If one operand is NULL and the other operand is either TRUE or NULL, the result is NULL (unknown).
ObjectDB supports the Java/JDO && operator as a synonym of AND as part of its JDO support.
Binary OR (||) Operator
The following query retrieves countries whose population or area exceeds a specified limit:
SELECT c FROM Country c WHERE c.population > :population OR c.area > :area
A valid operand of an OR operator must be one of: TRUE, FALSE, and NULL.
The following table shows how the OR operator is evaluated based on its two operands:
| TRUE | FALSE | NULL | |
|---|---|---|---|
| TRUE | TRUE | TRUE | TRUE |
| FALSE | TRUE | FALSE | NULL |
| NULL | TRUE | NULL | NULL |
NULL represents unknown. Therefore, if one operand is NULL and the other operand is TRUE the result is TRUE, because one TRUE operand is sufficient for a TRUE result. If one operand is NULL and the other operand is either FALSE or NULL, the result is NULL (unknown).
ObjectDB supports the Java/JDO || operator as a synonym for OR as part of its JDO support.
Unary NOT (!) Operator
The following query retrieves all the countries whose population does not exceed a specified limit:
SELECT c FROM Country c WHERE NOT (c.population > :population)
The operand of a NOT operator must be one of: TRUE, FALSE, or NULL.
The following table shows how the NOT operator is evaluated based on its operand:
| TRUE | FALSE | NULL |
|---|---|---|
| FALSE | TRUE | NULL |
If the operand is NULL, which represents unknown, the result is also NULL (unknown).
ObjectDB also supports the Java/JDO ! operator as a replacement for NOT as part of its JDO support.
Criteria Query Logical Operators
Boolean Expressions and Predicates
Boolean expressions are represented in criteria queries by Expressionjakarta.persistence.criteria.Expression
Pathjakarta.persistence.criteria.Path- JPA Interface Represents a simple or compound attribute path from a bound type or collection, and is a "primitive" expression.<Boolean> isInUN = country.getPath .get(attributeName) - JPA Method Create a path corresponding to the referenced attribute.("isInUN"); Pathjakarta.persistence.criteria.Path - JPA Interface Represents a simple or compound attribute path from a bound type or collection, and is a "primitive" expression.<Boolean> isInEU = country.getPath .get(attributeName) - JPA Method Create a path corresponding to the referenced attribute.("isInEU"); Pathjakarta.persistence.criteria.Path - JPA Interface Represents a simple or compound attribute path from a bound type or collection, and is a "primitive" expression.<Boolean> isInOECD = country.getPath .get(attributeName) - JPA Method Create a path corresponding to the referenced attribute.("isInOECD");
Predicatejakarta.persistence.criteria.Predicate - JPA Interface The type of a simple or compound predicate: a conjunction or disjunction of restrictions. is a special sub interface of Expressionjakarta.persistence.criteria.Expression
Predicatejakarta.persistence.criteria.Predicate - JPA Interface The type of a simple or compound predicate: a conjunction or disjunction of restrictions. isLarge = cb.gtCriteriaBuilder.gt(x,y) - JPA Method Create a predicate for testing whether the first argument is greater than the second.(country.getPath.get(attributeName) - JPA Method Create a path corresponding to the referenced attribute.("area"), 1000000);
AND / OR Expressions
The CriteriaBuilder interface provides factory methods that take two Expressionjakarta.persistence.criteria.ExpressionPredicate instance:
Predicatejakarta.persistence.criteria.Predicate - JPA Interface The type of a simple or compound predicate: a conjunction or disjunction of restrictions. p1 = cb.andCriteriaBuilder.and(x,y) - JPA Method Create a conjunction of the given boolean expressions.(isInUN, isInEU); // Member of both UN and EU Predicatejakarta.persistence.criteria.Predicate - JPA Interface The type of a simple or compound predicate: a conjunction or disjunction of restrictions. p2 = cb.orCriteriaBuilder.or(x,y) - JPA Method Create a disjunction of the given boolean expressions.(isInOECD, isLarge); // Either OECD member or large
Additional factory methods are available for a variant number of predicates:
Predicatejakarta.persistence.criteria.Predicate - JPA Interface The type of a simple or compound predicate: a conjunction or disjunction of restrictions. p3 = cb.andCriteriaBuilder.and(restrictions) - JPA Method Create a conjunction of the given restriction predicates.(p1, isLarge, cb.isTrueCriteriaBuilder.isTrue(x) - JPA Method Create a predicate testing for a true value.(isInOECD)); Predicatejakarta.persistence.criteria.Predicate - JPA Interface The type of a simple or compound predicate: a conjunction or disjunction of restrictions. p4 = cb.orCriteriaBuilder.or(restrictions) - JPA Method Create a disjunction of the given restriction predicates.(p2, cb.isTrueCriteriaBuilder.isTrue(x) - JPA Method Create a predicate testing for a true value.(isInUN), cb.isTrueCriteriaBuilder.isTrue(x) - JPA Method Create a predicate testing for a true value.(isInEU));
In the above code non Predicate boolean expressions are converted to Predicate instances using the isTrueCriteriaBuilder.isTrue(x) - JPA Method Create a predicate testing for a true value. method. This is required because in the non binary version the factory methods accept only Predicate instances as arguments.
NOT Expression
There are two ways to create a NOT operator:
Predicatejakarta.persistence.criteria.Predicate - JPA Interface The type of a simple or compound predicate: a conjunction or disjunction of restrictions. p5 = cb.notCriteriaBuilder.not(restriction) - JPA Method Create a negation of the given restriction.(isInUN); Predicatejakarta.persistence.criteria.Predicate - JPA Interface The type of a simple or compound predicate: a conjunction or disjunction of restrictions. p6 = isLarge.notPredicate.not() - JPA Method Create a negation of the predicate.();
The CriteriaBuilder's notCriteriaBuilder.not(restriction) - JPA Method Create a negation of the given restriction. method creates a Predicate by negating a specified boolean expression. Alternatively, to create a negation of a Predicate instance, the Predicate's notPredicate.not() - JPA Method Create a negation of the predicate. method can be invoked.