6.17.4 FROM clause

This subsection describes the FROM clause.

Organization of this subsection
(1) Range variable declaration and identification variables
(2) Path expression
(3) Joins expression
(4) Declaring the collection members
(5) Notes

(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:

(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 expressionContentsSyntax name of BNF syntax #1
Inner JoinsThis expression binds two entity classes and extracts only the entity objects with relation, in the fields to be related.join, join_spec
Left Outer JoinsThis 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 JoinsThis 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. #2fetch_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.