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.

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 Expressionjakarta.persistence.criteria.ExpressionType for query expressions.<Boolean> and descendant interfaces. For example, a boolean path (a field or a 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");

Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. is a special sub interface of Expressionjakarta.persistence.criteria.ExpressionType for query expressions.<Boolean> that represents many operator and function expressions whose type is boolean - 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 take 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 for a variant 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 above code non Predicate boolean expressions are converted to Predicate instances using the isTruejakarta.persistence.criteria.CriteriaBuilder.isTrue(Expression)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.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 CriteriaBuilder's notjakarta.persistence.criteria.CriteriaBuilder.not(Expression)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 notjakarta.persistence.criteria.Predicate.not()Create a negation of the predicate. method can be invoked.