Logical Operators in JPQL and Criteria API
Logical operators in JPQL and JPA criteria queries combine simple Boolean expressions to form complex expressions.
Logical operators
ObjectDB supports two sets of logical operators, as shown in the following table:
| Set 1: JPQL / SQL | Set 2: Java / JDO |
|---|---|
| AND | && |
| OR | || |
| NOT | ! |
JPQL uses SQL notation, while Java uses its own notation, which is also used by the JDO Query Language (JDOQL). ObjectDB supports both notations.
Binary AND (&&) operator
The following query retrieves countries whose population and area exceed specified limits:
SELECT c FROM Country c WHERE c.population > :population AND c.area > :area
Valid operands for the AND operator are TRUE, FALSE, or 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 an unknown value. Therefore, if one operand is NULL and the other is FALSE, the result is FALSE because one FALSE operand is sufficient to make the expression false. If one operand is NULL and the other is TRUE or NULL, the result is NULL.
As part of its JDO support, ObjectDB also supports the Java/JDO && operator as a synonym for AND.
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
Valid operands for the OR operator are TRUE, FALSE, or 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 an unknown value. Therefore, if one operand is NULL and the other is TRUE, the result is TRUE because one TRUE operand is sufficient to make the expression true. If one operand is NULL and the other is FALSE or NULL, the result is NULL.
As part of its JDO support, ObjectDB also supports the Java/JDO || operator as a synonym for OR.
Unary NOT (!) operator
The following query retrieves all countries whose population does not exceed a specified limit:
SELECT c FROM Country c WHERE NOT (c.population > :population)
Valid operands for the NOT operator are 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 an unknown value, the result is also NULL.
As part of its JDO support, ObjectDB also supports the Java/JDO ! operator as a synonym for NOT.
Criteria query logical operators
Boolean expressions and predicates
In criteria queries, Boolean expressions are represented by the Expressionjakarta.persistence.criteria.ExpressionType for query expressions.<Boolean> interface and its descendants. For example, a Boolean path (a field or property) is represented by Pathjakarta.persistence.criteria.PathRepresents a simple or compound attribute path from a bound type or collection, and is a "primitive" expression.<Boolean>:
Pathjakarta.persistence.criteria.PathRepresents a simple or compound attribute path from a bound type or collection, and is a "primitive" expression.<Boolean> isInUN = country.getjakarta.persistence.criteria.Path.get(String)Create a path corresponding to the referenced attribute.("isInUN"); Pathjakarta.persistence.criteria.PathRepresents a simple or compound attribute path from a bound type or collection, and is a "primitive" expression.<Boolean> isInEU = country.getjakarta.persistence.criteria.Path.get(String)Create a path corresponding to the referenced attribute.("isInEU"); Pathjakarta.persistence.criteria.PathRepresents a simple or compound attribute path from a bound type or collection, and is a "primitive" expression.<Boolean> isInOECD = country.getjakarta.persistence.criteria.Path.get(String)Create a path corresponding to the referenced attribute.("isInOECD");
The Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. interface is a subinterface of Expressionjakarta.persistence.criteria.ExpressionType for query expressions.<Boolean> that represents expressions that return a Boolean value, such as comparison operators:
Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. isLarge = cb.gtjakarta.persistence.criteria.CriteriaBuilder.gt(Expression,Number)Create a predicate for testing whether the first argument is greater than the second.(country.getjakarta.persistence.criteria.Path.get(String)Create a path corresponding to the referenced attribute.("area"), 1000000);
AND / OR expressions
The CriteriaBuilder interface provides factory methods that accept two Expressionjakarta.persistence.criteria.ExpressionType for query expressions.<Boolean> operands (including Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. instances) and return a new Predicate instance:
Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. p1 = cb.andjakarta.persistence.criteria.CriteriaBuilder.and(Expression,Expression)Create a conjunction of the given boolean expressions.(isInUN, isInEU); // Member of both UN and EU Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. p2 = cb.orjakarta.persistence.criteria.CriteriaBuilder.or(Expression,Expression)Create a disjunction of the given boolean expressions.(isInOECD, isLarge); // Either OECD member or large
Additional factory methods are available that accept a variable number of predicates:
Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. p3 = cb.andjakarta.persistence.criteria.CriteriaBuilder.and(Predicate...)Create a conjunction of the given restriction predicates.(p1, isLarge, cb.isTruejakarta.persistence.criteria.CriteriaBuilder.isTrue(Expression)Create a predicate testing for a true value.(isInOECD)); Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. p4 = cb.orjakarta.persistence.criteria.CriteriaBuilder.or(Predicate...)Create a disjunction of the given restriction predicates.(p2, cb.isTruejakarta.persistence.criteria.CriteriaBuilder.isTrue(Expression)Create a predicate testing for a true value.(isInUN), cb.isTruejakarta.persistence.criteria.CriteriaBuilder.isTrue(Expression)Create a predicate testing for a true value.(isInEU));
In the preceding code, non-Predicate Boolean expressions are converted to Predicate instances by using the isTruejakarta.persistence.criteria.CriteriaBuilder.isTrue(Expression)Create a predicate testing for a true value. method. This conversion is required because the non-binary versions of the factory methods accept only Predicate instances as arguments.
NOT expression
There are two ways to create a NOT expression:
Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. p5 = cb.notjakarta.persistence.criteria.CriteriaBuilder.not(Expression)Create a negation of the given restriction.(isInUN); Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. p6 = isLarge.notjakarta.persistence.criteria.Predicate.not()Create a negation of the predicate.();
The notjakarta.persistence.criteria.CriteriaBuilder.not(Expression)Create a negation of the given restriction. method of the CriteriaBuilder interface creates a Predicate by negating a specified Boolean expression. Alternatively, to negate a Predicate instance, you can invoke its notjakarta.persistence.criteria.Predicate.not()Create a negation of the predicate. method.