Collections in JPQL and Criteria Queries
Collections may appear in JPQL queries:
- as parameters - when collections are assigned as arguments.
- as path expressions - in navigation to persistent collection fields.
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 or not.
For example:
c.languages IS EMPTYisTRUEif the collection is empty andFALSEotherwise.c.languages IS NOT EMPTYis FALSE if the collection is empty andTRUEotherwise.
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.languagesisTRUEiflanguagescontains'English'andFALSEif not.
'English' NOT MEMBER OF c.languagesisTRUEiflanguagesdoes 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 :languagesisTRUEif the argument for thelanguagesparameter is a collection that contains'English'andFALSEif not.'English' NOT IN :languagesisTRUEif the argument for thelanguagesparameter 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 CriteriaBuilderjakarta.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: Expressionjakarta.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"); Expressionjakarta.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 Predicatejakarta.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); Predicatejakarta.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 Predicatejakarta.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); Predicatejakarta.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); Predicatejakarta.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); Predicatejakarta.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: Predicatejakarta.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); Predicatejakarta.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) Expressionjakarta.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);