10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

SELECT c<br />

FROM Customer c INNER JOIN c.orders o<br />

WHERE c.status = 1 AND o.totalPrice > 10000<br />

<strong>The</strong>se examples are equivalent to the following query, which uses the IN operator:<br />

SELECT c<br />

FROM Customer c, IN(c.orders) o<br />

WHERE c.status = 1 AND o.totalPrice > 10000<br />

You can also join a single-valued relationship.<br />

SELECT t<br />

FROM Team t JOIN t.league l<br />

WHERE l.sport = :sport<br />

A LEFT JOIN or LEFT OUTER JOIN retrieves a set of entities where matching values in the join<br />

condition may be absent. <strong>The</strong> OUTER keyword is optional.<br />

SELECT c.name, o.totalPrice<br />

FROM Order o LEFT JOIN o.customer c<br />

A FETCH JOIN is a join operation that returns associated entities as a side-effect of running the<br />

query. In the following example, the query returns a set of departments, and as a side-effect, the<br />

associated employees of the departments, even though the employees were not explicitly<br />

retrieved by the SELECT clause.<br />

SELECT d<br />

FROM Department d LEFT JOIN FETCH d.employees<br />

WHERE d.deptno = 1<br />

Path Expressions<br />

Path expressions are important constructs in the syntax of the query language, for several<br />

reasons. First, they define navigation paths through the relationships in the abstract schema.<br />

<strong>The</strong>se path definitions affect both the scope and the results of a query. Second, they can appear<br />

in any of the main clauses of a query (SELECT, DELETE, HAVING, UPDATE, WHERE, FROM, GROUP BY,<br />

ORDER BY). Finally, although much of the query language is a subset of SQL, path expressions<br />

are extensions not found in SQL.<br />

Examples of Path Expressions<br />

Full Query Language Syntax<br />

Here, the WHERE clause contains a single_valued_path_expression. <strong>The</strong> p is an identification<br />

variable, and salary is a persistent field of Player.<br />

Chapter 27 • <strong>The</strong> <strong>Java</strong> Persistence Query Language 747

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!