2.10.2.3.2. JPQL Identification Variables
An identification variable is a valid identifier declared in the FROM clause of a query.
All identification variables must be declared in the FROM clause.
Identification variables cannot be declared in other clauses.
An identification variable must not be a reserved identifier or have the same name as any entity in the same persistence unit.
Identification variables are case insensitive.
An identification variable evaluates to a value of the type of the expression used in declaring the variable. For example, consider the previous query:
SELECT DISTINCT mag FROM Magazine mag JOIN mag.articles art JOIN art.author auth WHERE auth.firstName = 'John'
In the FROM clause declaration
mag.articles art, the identification variable art evaluates to any Article value directly reachable from Magazine.
The association-field articles is a collection of instances of the abstract schema type Article and the identification variable art refers to an element of this collection.
The type of auth is the abstract schema type of Author.
An identification variable can range over an entity, embeddable, or basic abstract schema type. An identification variable designates an instance of an entity abstract schema type or an element of a collection of entity abstract schema type instances.
Note that for identification variables referring to an instance of an association or collection represented as a java.util.Map, the identification variable is of the abstract schema type of the map value.
An identification variable always designates a reference to a single value.
It is declared in one of three ways: in a range variable declaration, in a join clause, or in a collection member declaration.
The identification variable declarations are evaluated from left to right in the FROM clause, and an identification variable declaration can use the result of a preceding identification variable declaration of the query string.
All identification variables used in the SELECT, WHERE, ORDER BY, GROUP BY, or HAVING clause of a SELECT or DELETE statement must be declared in the FROM clause.
The identification variables used in the WHERE clause of an UPDATE statement must be declared in the UPDATE clause.
Identification variables are existentially quantified in these clauses. This means that an identification variable represents a member of a collection or an instance of an entity’s abstract schema type. An identification variable never designates a collection in its entirety.
An identification variable is scoped to the query (or subquery) in which it is defined and is also visible to any subqueries within that query scope that do not define an identification variable of the same name.