Collections in JPQL and Criteria Queries

Collections can appear in JPQL queries in the following ways:

IS [NOT] EMPTY

The IS [NOT] EMPTY operator checks whether a specified collection is empty.

For example:

  • c.languages IS EMPTY evaluates to TRUE if the collection is empty and FALSE otherwise.
  • c.languages IS NOT EMPTY evaluates to TRUE if the collection is not empty and FALSE otherwise.

SIZE

The SIZE(collection) function returns the number of elements in a specified collection.

For example:

  • SIZE(c.languages) returns the number of elements in the languages collection.

[NOT] MEMBER [OF]

The [NOT] MEMBER OF operator checks whether a specified element is a member of a specified persistent collection field.

For example:

  • 'English' MEMBER OF c.languages evaluates to TRUE if the languages collection contains 'English', and FALSE otherwise.
  • 'English' NOT MEMBER OF c.languages evaluates to TRUE if the languages collection does not contain 'English', and FALSE otherwise.

[NOT] IN

The [NOT] IN operator provides an additional method for checking if a specified element is contained in a collection.

JPA specifies that you should use the MEMBER OF operator to check a collection field and the IN operator to check other collections, such as a collection passed as a query parameter.

For example:

  • 'English' IN :languages evaluates to TRUE if the languages parameter collection contains 'English', and FALSE otherwise.
  • 'English' NOT IN :languages evaluates to TRUE if the languages parameter collection does not contain 'English', and FALSE otherwise.

As an extension to standard JPQL, ObjectDB allows you to use both the IN and MEMBER OF operators with any type of collection. In ObjectDB, these operators are synonyms.

Criteria query collection expressions

The JPQL collection operators and functions are also available as JPA criteria query expressions. The CriteriaBuilderjakarta.persistence.criteria.CriteriaBuilderUsed to construct criteria queries, compound selections, expressions, predicates, orderings. interface provides factory methods for building these expressions:

  // Create path and parameter expressions:
  Expressionjakarta.persistence.criteria.ExpressionType for query expressions.<Collection<String>> languages = country.getjakarta.persistence.criteria.Path.get(String)Create a path corresponding to the referenced attribute.("languages");
  Expressionjakarta.persistence.criteria.ExpressionType for query expressions.<String> param = cb.parameterjakarta.persistence.criteria.CriteriaBuilder.parameter(Class)Create a parameter expression.(String.class);
  // collection IS [NOT] EMPTY
  Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. e1 = cb.isEmptyjakarta.persistence.criteria.CriteriaBuilder.isEmpty(Expression)Create a predicate that tests whether a collection is empty.(languages);
  Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. e2 = cb.isNotEmptyjakarta.persistence.criteria.CriteriaBuilder.isNotEmpty(Expression)Create a predicate that tests whether a collection is not empty.(languages);
  // element [NOT] MEMBER OF collection
  Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. m1 = cb.isMemberjakarta.persistence.criteria.CriteriaBuilder.isMember(Expression,Expression)Create a predicate that tests whether an element is a member of a collection.(param, languages);
  Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. m2 = cb.isMemberjakarta.persistence.criteria.CriteriaBuilder.isMember(E,Expression)Create a predicate that tests whether an element is a member of a collection.("English", languages);
  Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. m3 = cb.isNotMemberjakarta.persistence.criteria.CriteriaBuilder.isNotMember(Expression,Expression)Create a predicate that tests whether an element is not a member of a collection.(param, languages);
  Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. m4 = cb.isNotMemberjakarta.persistence.criteria.CriteriaBuilder.isNotMember(E,Expression)Create a predicate that tests whether an element is not a member of a collection.("French", languages);
  // element [NOT] IN collection:
  Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. i1 = param.injakarta.persistence.criteria.Expression.in(Object...)Create a predicate to test whether the expression is a member of the argument list.(languages);
  Predicatejakarta.persistence.criteria.PredicateThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. i2 = param.injakarta.persistence.criteria.Expression.in(Expression)Create a predicate to test whether the expression is a member of the collection.("English", "French");
  // SIZE(collection)
  Expressionjakarta.persistence.criteria.ExpressionType for query expressions.<Integer> size = cb.sizejakarta.persistence.criteria.CriteriaBuilder.size(Expression)Create an expression that tests the size of a collection.(languages);