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 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 Expressionjavax.persistence.criteria.Expression
Pathjavax.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.get("isInUN"); Pathjavax.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.get("isInEU"); Pathjavax.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.get("isInOECD");
Predicatejavax.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 Expressionjavax.persistence.criteria.Expression
Predicatejavax.persistence.criteria.Predicate (JPA Interface)The type of a simple or compound predicate: a conjunction or disjunction of restrictions. isLarge = cb.gt(country.get("area"), 1000000);
AND / OR Expressions
The CriteriaBuilder interface provides factory methods that take two Expressionjavax.persistence.criteria.ExpressionPredicate
instance:
Predicatejavax.persistence.criteria.Predicate (JPA Interface)The type of a simple or compound predicate: a conjunction or disjunction of restrictions. p1 = cb.and(isInUN, isInEU); // Member of both UN and EU Predicatejavax.persistence.criteria.Predicate (JPA Interface)The type of a simple or compound predicate: a conjunction or disjunction of restrictions. p2 = cb.or(isInOECD, isLarge); // Either OECD member or large
Additional factory methods are available for a variant number of predicates:
Predicatejavax.persistence.criteria.Predicate (JPA Interface)The type of a simple or compound predicate: a conjunction or disjunction of restrictions. p3 = cb.and(p1, isLarge, cb.isTrue(isInOECD)); Predicatejavax.persistence.criteria.Predicate (JPA Interface)The type of a simple or compound predicate: a conjunction or disjunction of restrictions. p4 = cb.or(p2, cb.isTrue(isInUN), cb.isTrue(isInEU));
In the above code non Predicate
boolean expressions are converted to Predicate
instances using the isTrue 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:
Predicatejavax.persistence.criteria.Predicate (JPA Interface)The type of a simple or compound predicate: a conjunction or disjunction of restrictions. p5 = cb.not(isInUN); Predicatejavax.persistence.criteria.Predicate (JPA Interface)The type of a simple or compound predicate: a conjunction or disjunction of restrictions. p6 = isLarge.not();
The CriteriaBuilder
's not method creates a Predicate
by negating a specified boolean expression. Alternatively, to create a negation of a Predicate
instance, the Predicate
's not method can be invoked.