Collections in JPQL and Criteria Queries

Collections may appear in JPQL queries:

IS [NOT] EMPTY

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

For example:

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

SIZE

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

For example:

  • SIZE(c.languages) is evaluated to the number of languages in that collection.

[NOT] MEMBER [OF]

The [NOT] MEMBER OF operator checks if a specified element is contained in a specified persistent collection field.

For example:

  • 'English' MEMBER OF c.languages is TRUE if languages contains 'English' and FALSE if not.
  • 'English' NOT MEMBER OF c.languages is TRUE if languages does not contain 'English'.

[NOT] IN

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

JPA distinguishes between the MEMBER OF operator, which should be used for checking a collection field, and the IN operator, which should be used for checking other collections, such as a collection that is passed to the query as a parameter.

For example:

  • 'English' IN :languages is TRUE if the argument for the languages parameter  is a collection that contains 'English' and FALSE if not.
  • 'English' NOT IN :languages is TRUE if the argument for the languages parameter  is a collection that doesn't contain 'English'.

ObjectDB enables as an extension to standard JPQL to use both operators (IN and MEMBER OF) with any type of collection, so in ObjectDB these operators are treated as synonyms.

Criteria Query Collection Expressions

JPQL collection operators and functions (which are described above) are available also as JPA criteria query expressions. The CriteriaBuilderjavax.persistence.criteria.CriteriaBuilder - JPA Interface Used to construct criteria queries, compound selections, expressions, predicates, orderings. interface provides factory methods for building these expressions, as shown in the following examples:

  // Create path and parameter expressions:
  Expressionjavax.persistence.criteria.Expression - JPA Interface
 Type for query expressions.<Collection<String>> languages = country.getPath.get(attributeName) - JPA Method
  Create a path corresponding to the referenced attribute.("languages");
  Expressionjavax.persistence.criteria.Expression - JPA Interface
 Type for query expressions.<String> param = cb.parameterCriteriaBuilder.parameter(paramClass) - JPA Method
 Create a parameter expression.(String.class);

  // collection IS [NOT] EMPTY
  Predicatejavax.persistence.criteria.Predicate - JPA Interface
 The type of a simple or compound predicate: a conjunction or
 disjunction of restrictions. e1 = cb.isEmptyCriteriaBuilder.isEmpty(collection) - JPA Method
  Create a predicate that tests whether a collection is empty.(languages);
  Predicatejavax.persistence.criteria.Predicate - JPA Interface
 The type of a simple or compound predicate: a conjunction or
 disjunction of restrictions. e2 = cb.isNotEmptyCriteriaBuilder.isNotEmpty(collection) - JPA Method
  Create a predicate that tests whether a collection is
  not empty.(languages);

  // element [NOT] MEMBER OF collection
  Predicatejavax.persistence.criteria.Predicate - JPA Interface
 The type of a simple or compound predicate: a conjunction or
 disjunction of restrictions. m1 = cb.isMemberCriteriaBuilder.isMember(elem,collection) - JPA Method
  Create a predicate that tests whether an element is
  a member of a collection.(param, languages);
  Predicatejavax.persistence.criteria.Predicate - JPA Interface
 The type of a simple or compound predicate: a conjunction or
 disjunction of restrictions. m2 = cb.isMemberCriteriaBuilder.isMember(elem,collection) - JPA Method
  Create a predicate that tests whether an element is
  a member of a collection.("English", languages);
  Predicatejavax.persistence.criteria.Predicate - JPA Interface
 The type of a simple or compound predicate: a conjunction or
 disjunction of restrictions. m3 = cb.isNotMemberCriteriaBuilder.isNotMember(elem,collection) - JPA Method
  Create a predicate that tests whether an element is
  not a member of a collection.(param, languages);
  Predicatejavax.persistence.criteria.Predicate - JPA Interface
 The type of a simple or compound predicate: a conjunction or
 disjunction of restrictions. m4 = cb.isNotMemberCriteriaBuilder.isNotMember(elem,collection) - JPA Method
  Create a predicate that tests whether an element is
  not a member of a collection.("French", languages);

  // element [NOT] IN collection:
  Predicatejavax.persistence.criteria.Predicate - JPA Interface
 The type of a simple or compound predicate: a conjunction or
 disjunction of restrictions. i1 = param.inExpression.in(values) - JPA Method
 Create a predicate to test whether the expression is a member
 of the argument list.(languages);
  Predicatejavax.persistence.criteria.Predicate - JPA Interface
 The type of a simple or compound predicate: a conjunction or
 disjunction of restrictions. i2 = param.inExpression.in(values) - JPA Method
 Create a predicate to test whether the expression is a member
 of the collection.("English", "French");

  // SIZE(collection)
  Expressionjavax.persistence.criteria.Expression - JPA Interface
 Type for query expressions.<Integer> size = cb.sizeCriteriaBuilder.size(collection) - JPA Method
 Create an expression that tests the size of a collection.(languages);