6.17.4 FROM clause
(1) Range variable declaration and identification variables
A range variable declaration is a declaration that codes the logical name of the entity class in the FROM clause and then specifies AS and the identifier (AS can be omitted). The identifier of this range variable declaration is called the identification variable. An example of range variable declaration and identification variable is as follows:
SELECT ... (omitted) ...
FROM Department AS dep
WHERE ... (omitted) ... |
The part 'Department AS dep' is the range variable declaration. Also, 'dep' is the identification variable. The syntax of the range variable declaration is as follows:
range_variable_declaration ::=
abstract_schema_name [AS] identification_variable |
The syntax of the identification variable in the range variable declaration is the same as the SQL syntax. A description of the syntax is as follows:
- The use of the keyword AS is optional.
- You cannot omit the identification variable. However, you can omit the AS specified between the abstract schema and the identification variable. Specify the identification variable in the FROM clause.
- A reserved identifier cannot be used. If used, an exception occurs.
- The same name as another entity in the same persistence unit cannot be used. With Cosminexus JPA Provider, the operations might not function properly if an entity with the same name is used.
- The identification variable is not case sensitive.
- You cannot specify the same name as the abstract schema name. With Cosminexus JPA Provider, the operations might not function properly if the abstract schema with the same name is specified.
- The syntax must begin with a Java identifier character and all the other characters must be partial characters of the Java identifier. If other characters are specified, an exception occurs. For the first character, use a character by which the return value of the Character.isJavaIdentifierStart method becomes true (including underscore (_) and dollar mark ($) characters). For the characters other than the first character, use a character by which the return value of the Character.isJavaIdentifierPart method becomes true (however, question mark (?) is a reserved word in JPQL and cannot be used).
- The reserved words in JPQL are as follows:
SELECT, FROM, WHERE, UPDATE, DELETE, JOIN, OUTER, INNER, LEFT, GROUP, BY, HAVING, FETCH, DISTINCT, OBJECT, NULL, TRUE, FALSE, NOT, AND, OR, BETWEEN, LIKE, IN, AS, UNKNOWN, EMPTY, MEMBER, OF, IS, AVG, MAX, MIN, SUM, COUNT, ORDER, BY, ASC, DESC, MOD, UPPER, LOWER, TRIM, POSITION, CHARACTER_LENGTH, CHAR_LENGTH, BIT_LENGTH, CURRENT_TIME, CURRENT_DATE, CURRENT_TIMESTAMP, NEW, EXISTS, ALL, ANY, and SOME
Note that UNKNOWN is not used in JPA 1.0, but is a reserved word in Cosminexus JPA Provider.
(2) Path expression
A path expression is an expression in which a period (.) is added after the identification variable and is used for continuing the state field or relation field. Therefore, the path expression type becomes the state field or relation field type.
You can build up a path expression further from the relation fields obtained by tracing the path expression. However, if the path expression type that forms the base is a collection relation field, you cannot build a path expression. Creating the path expression from the collection type becomes a structural error.
Note that if the relation field in the middle of the path expression is a null value, the path assumes that the value does not exist, so the query result is not affected.
You can use the path expression with the syntax using inner join. For details on the path expression syntax, see Appendix D BNF for JPQL.
- Reference note
- The types of relation fields are as follows:
- A collection relation field (collection_valued_association_field) is one in which the relation field is specified using a collection. The collection relation field is indicated by OneToMany or ManyToMany relation.
- A non-collection relation field (single_valued_association_field) is one in which the relation field is specified using single-valued. The non-collection relation field is indicated by OneToOne or ManyToOne relation.
- The embedded class field is the field name of the entity corresponding to the embedded class.
(3) Joins expression
The Joins expression is available in the FROM clause. The following table lists and describes the available Joins expressions.
Table 6-20 Joins expressions available in the FROM clause
Joins expression | Contents | Syntax name of BNF syntax #1 |
---|
Inner Joins | This expression binds two entity classes and extracts only the entity objects with relation, in the fields to be related. | join, join_spec |
Left Outer Joins | This expression binds two entity classes and extracts the entity objects with relation as well as the entity objects without relation, in the fields to be related. | join, join_spec |
Fetch Joins | This expression binds two entity classes in the related fields. Note that a relation exists between the entities due to relationship, so specify only one entity class in the Select clause. #2 | fetch_join |
#1 For details on the syntax name of the BNF syntax, see Appendix D BNF for JPQL.
#2 When the entity is obtained using Fetch Joins, the information about the relation destination entity specified on the right is obtained at the same time as the execution of the query. As a result, you can obtain the relation destination information without dependence on the fetch strategy. An example of coding Fetch Joins is as follows:
SELECT emp FROM Employee AS emp JOIN FETCH emp.company |
- Notes on using the Join expression
- The notes on using the Join expression are as follows:
- Notes on Inner Joins
- The keyword INNER is used optionally.
- Notes on Left Outer Joins
- The keyword OUTER is used optionally.
- Notes on Fetch Joins
- With Fetch Joins, the entity information of two entities is specified in one entity. The information of the specified entity and the information of another entity related to that entity are bound.
Note that in Cosminexus JPA Provider, you specify entities with relationship because the entities are bound by one entity information. If entities without relationship are specified, an exception occurs.
- The relation referenced on the right side of JOIN FETCH must be a relation belonging to the entity returned as a query result. In Cosminexus JPA Provider, if the relation does not belong to the entity, an exception occurs.
- An identifier cannot be specified in the entity referenced on the right side of JOIN FETCH. Therefore, the entity cannot be referenced in the query.
(4) Declaring the collection members
The identification variables for declaring the collection members are declared by using the reserved identifier IN. You can obtain the collection value for the identification variable defined in the collection member expression, by using the path expression. An example of coding the collection member expression is as follows:
SELECT emp.employeeId, emp.employeeName, dep.departmentName
FROM Department AS dep, IN (dep.employees) AS emp
WHERE dep.departmentId = 3 |
For details on the syntax of the collection member expression, see Appendix D BNF for JPQL.
(5) Notes
This section describes the notes on the FROM clause.
- Effect of the identification variable
Even if the identification variable declared in the FROM clause is not used in the WHERE clause, the declared identification variable is applied to the query result.
- Notes on polymorphism
JPQL is a polymorphism. The specific entity class instances referenced explicitly by the FROM clause as well as the subclasses become the target. Therefore, the instances that can be obtained using a query include subclass instances that satisfy the query conditions.