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] EMPTY
The IS [NOT] EMPTY operator checks whether a specified collection is empty or not.
For example:
c.languages IS EMPTY
isTRUE
if the collection is empty andFALSE
otherwise.c.languages IS NOT EMPTY
is FALSE if the collection is empty andTRUE
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
isTRUE
iflanguages
contains'English'
andFALSE
if not.
'English' NOT MEMBER OF c.languages
isTRUE
iflanguages
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
isTRUE
if the argument for thelanguages
parameter is a collection that contains'English'
andFALSE
if not.'English' NOT IN :languages
isTRUE
if the argument for thelanguages
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 InterfaceUsed 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 MethodCreate a parameter expression.(String.class); // collection IS [NOT] EMPTY Predicatejavax.persistence.criteria.Predicate - JPA InterfaceThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. e1 = cb.isEmptyCriteriaBuilder.isEmpty(collection) - JPA MethodCreate a predicate that tests whether a collection is empty.(languages); Predicatejavax.persistence.criteria.Predicate - JPA InterfaceThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. e2 = cb.isNotEmptyCriteriaBuilder.isNotEmpty(collection) - JPA MethodCreate a predicate that tests whether a collection is not empty.(languages); // element [NOT] MEMBER OF collection Predicatejavax.persistence.criteria.Predicate - JPA InterfaceThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. m1 = cb.isMemberCriteriaBuilder.isMember(elem,collection) - JPA MethodCreate a predicate that tests whether an element is a member of a collection.(param, languages); Predicatejavax.persistence.criteria.Predicate - JPA InterfaceThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. m2 = cb.isMemberCriteriaBuilder.isMember(elem,collection) - JPA MethodCreate a predicate that tests whether an element is a member of a collection.("English", languages); Predicatejavax.persistence.criteria.Predicate - JPA InterfaceThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. m3 = cb.isNotMemberCriteriaBuilder.isNotMember(elem,collection) - JPA MethodCreate a predicate that tests whether an element is not a member of a collection.(param, languages); Predicatejavax.persistence.criteria.Predicate - JPA InterfaceThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. m4 = cb.isNotMemberCriteriaBuilder.isNotMember(elem,collection) - JPA MethodCreate 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 InterfaceThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. i1 = param.inExpression.in(values) - JPA MethodCreate a predicate to test whether the expression is a member of the argument list.(languages); Predicatejavax.persistence.criteria.Predicate - JPA InterfaceThe type of a simple or compound predicate: a conjunction or disjunction of restrictions. i2 = param.inExpression.in(values) - JPA MethodCreate 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 MethodCreate an expression that tests the size of a collection.(languages);