background image

Collection Member Expressions

<< LIKE Expressions | EXISTS Expressions >>
<< LIKE Expressions | EXISTS Expressions >>

Collection Member Expressions

SELECT t
FROM Team t
WHERE t.league = NULL
The comparison with NULL using the equals operator (=) always returns an unknown value,
even if the relationship is not set. The second query will always return an empty result.
Empty Collection Comparison Expressions
The IS [NOT] EMPTY comparison expression tests whether a collection-valued path expression
has no elements. In other words, it tests whether or not a collection-valued relationship has
been set.
If the collection-valued path expression is NULL, then the empty collection comparison
expression has a NULL value.
Here is an example that finds all orders that do not have any line items:
SELECT o
FROM Order o
WHERE o.lineItems IS EMPTY
Collection Member Expressions
The [NOT] MEMBER [OF] collection member expression determines whether a value is a member
of a collection. The value and the collection members must have the same type.
If either the collection-valued or single-valued path expression is unknown, then the collection
member expression is unknown. If the collection-valued path expression designates an empty
collection, then the collection member expression is FALSE.
The OF keyword is optional.
The following example tests whether a line item is part of an order:
SELECT o
FROM Order o
WHERE :lineItem MEMBER OF o.lineItems
Subqueries
Subqueries may be used in the WHERE or HAVING clause of a query. Subqueries must be
surrounded by parentheses.
The following example find all customers who have placed more than 10 orders:
SELECT c
FROM Customer c
WHERE (SELECT COUNT(o) FROM c.orders o) > 10
Full Query Language Syntax
Chapter 27 · The Java Persistence Query Language
755