Chapter 15. The query languages

 

In this chapter

  • Writing JPQL and criteria queries
  • Retrieving data efficiently with joins
  • Reporting queries and subselects

Queries are the most interesting part of writing good data access code. A complex query may require a long time to get right, and its impact on the performance of the application can be tremendous. On the other hand, writing queries becomes much easier with more experience, and what seemed difficult at first is only a matter of knowing the available query languages.

This chapter covers the query languages available in JPA: JPQL and the criteria query API. We always show the same query example with both languages/API, where the result of the queries is equivalent.

Major new features in JPA 2
  • There is now support for CASE, NULLIF, and COALESCE operators, with the same semantics as their SQL counterparts.
  • You can downcast with the TREAT operator in restrictions and selections.
  • You can call arbitrary SQL database functions in restrictions and projections.
  • You can append additional join conditions for outer joins with the new ON keyword.
  • You can use joins in subselect FROM clauses.

15.1. Selection

15.2. Restriction

15.3. Projection

15.4. Joins

15.5. Subselects

15.6. Summary