background image

Identification Variables

<< Query Language Identifier | Collection Member Declarations >>
<< Query Language Identifier | Collection Member Declarations >>

Identification Variables

Identification Variables
An identification variable is an identifier declared in the FROM clause. Although the SELECT and
WHERE
clauses can reference identification variables, they cannot declare them. All identification
variables must be declared in the FROM clause.
Because an identification variable is an identifier, it has the same naming conventions and
restrictions as an identifier with the exception that an identification variables is case-insensitive.
For example, an identification variable cannot be the same as a query language keyword. (See
the preceding section for more naming rules.) Also, within a given persistence unit, an
identification variable name must not match the name of any entity or abstract schema.
The FROM clause can contain multiple declarations, separated by commas. A declaration can
reference another identification variable that has been previously declared (to the left). In the
following FROM clause, the variable t references the previously declared variable p:
FROM Player p, IN (p.teams) AS t
Even if an identification variable is not used in the WHERE clause, its declaration can affect the
results of the query. For an example, compare the next two queries. The following query returns
all players, whether or not they belong to a team:
SELECT p
FROM Player p
In contrast, because the next query declares the t identification variable, it fetches all players
that belong to a team:
SELECT p
FROM Player p, IN (p.teams) AS t
The following query returns the same results as the preceding query, but the WHERE clause makes
it easier to read:
SELECT p
FROM Player p
WHERE p.teams IS NOT EMPTY
An identification variable always designates a reference to a single value whose type is that of
the expression used in the declaration. There are two kinds of declarations: range variable and
collection member.
Range Variable Declarations
To declare an identification variable as an abstract schema type, you specify a range variable
declaration. In other words, an identification variable can range over the abstract schema type
of an entity. In the following example, an identification variable named p represents the abstract
schema named Player:
Full Query Language Syntax
Chapter 27 · The Java Persistence Query Language
747