Java Reference
In-Depth Information
Chapter 11. JPQL
This chapter covers
• Creating and executing queries
• The Java Persistence Query Language
• Leveraging the criteria API
• Using SQL queries
• Invoking stored procedures
Chapter 10 introduced the basics of the EntityManager API. You saw how to get an
instance of EntityManager from the container and use it to perform the basic CRUD
operations (create, read, update, delete) on entities. Create, update, and delete are relatively
simple operations that chapter 10 covered in full. Read, however, is much more complicated
because of the variety of ways to query database data.
In this chapter we'll explore read in depth by looking at the Java Persistence Query Lan-
guage (JPQL), the criteria API, and native SQL. Each method tackles the problem of query-
ing objects in a relational database using a slightly different approach and addresses a differ-
ent set of concerns and problems. We'll round out the chapter looking at support for invok-
ing stored procedures—a new feature in Java EE 7. Let's start this chapter by diving right in
to JPQL.
11.1. Introducing JPQL
The meat of this chapter covers the ins and outs of JPQL. We'll start with a definition of
the language, provide numerous examples illustrating almost every aspect, and include some
little-known tips along the way.
There was little debate among the EJB 3 Expert Group on which to use as the standard query
language for JPA. They agreed to use JPQL. JPQL is an extension of EJB QL, the query lan-
guage of EJB 2. It didn't make sense to invent yet another language for such a well-known
domain, so the group voted unanimously to make EJB QL the query language of choice and
to address all its previous limitations. The use of JPQL will make the migration of EJB 2
entity beans to EJB 3 persistence easier.
Search WWH ::




Custom Search