Collections in JPQL and Criteria Queries
Collections can appear in JPQL queries in the following ways:
- As parameters when a collection is passed as an argument.
- As path expressions when navigating to a persistent collection field.
This page covers the following topics:
IS [NOT] EMPTYSIZE[NOT] MEMBER [OF][NOT] INCriteria query collection expressionsIS [NOT] EMPTY
The IS [NOT] EMPTY operator checks whether a specified collection is empty.
For example:
c.languages IS EMPTYevaluates toTRUEif the collection is empty andFALSEotherwise.c.languages IS NOT EMPTYevaluates toTRUEif the collection is not empty andFALSEotherwise.
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 thelanguagescollection.
[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.languagesevaluates toTRUEif thelanguagescollection contains'English', andFALSEotherwise.'English' NOT MEMBER OF c.languagesevaluates toTRUEif thelanguagescollection does not contain'English', andFALSEotherwise.
[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 :languagesevaluates toTRUEif thelanguagesparameter collection contains'English', andFALSEotherwise.'English' NOT IN :languagesevaluates toTRUEif thelanguagesparameter collection does not contain'English', andFALSEotherwise.
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);