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 Language (JPQL), the criteria API, and native SQL. Each method tackles the problem of querying objects in a relational database using a slightly different approach and addresses a different set of concerns and problems. We’ll round out the chapter looking at support for invoking 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.

11.2. Criteria queries

11.3. Native queries

11.4. Summary