background image

Path Expressions

<< Collection Member Declarations | Expression Types >>
<< Collection Member Declarations | Expression Types >>

Path Expressions

SELECT c
FROM Customer c INNER JOIN c.orders o
WHERE c.status = 1 AND o.totalPrice > 10000
These examples are equivalent to the following query, which uses the IN operator:
SELECT c
FROM Customer c, IN(c.orders) o
WHERE c.status = 1 AND o.totalPrice > 10000
You can also join a single-valued relationship.
SELECT t
FROM Team t JOIN t.league l
WHERE l.sport = :sport
A LEFT JOIN or LEFT OUTER JOIN retrieves a set of entities where matching values in the join
condition may be absent. The OUTER keyword is optional.
SELECT c.name, o.totalPrice
FROM Order o LEFT JOIN o.customer c
A FETCH JOIN is a join operation that returns associated entities as a side-effect of running the
query. In the following example, the query returns a set of departments, and as a side-effect, the
associated employees of the departments, even though the employees were not explicitly
retrieved by the SELECT clause.
SELECT d
FROM Department d LEFT JOIN FETCH d.employees
WHERE d.deptno = 1
Path Expressions
Path expressions are important constructs in the syntax of the query language, for several
reasons. First, they define navigation paths through the relationships in the abstract schema.
These path definitions affect both the scope and the results of a query. Second, they can appear
in any of the main clauses of a query (SELECT, DELETE, HAVING, UPDATE, WHERE, FROM, GROUP BY,
ORDER BY
). Finally, although much of the query language is a subset of SQL, path expressions
are extensions not found in SQL.
Examples of Path Expressions
Here, the WHERE clause contains a single_valued_path_expression. The p is an identification
variable, and salary is a persistent field of Player.
Full Query Language Syntax
Chapter 27 · The Java Persistence Query Language
749