From the course: Java Persistence API (JPA): 2 Inheritance and Querying

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Create queries with Criteria API

Create queries with Criteria API

From the course: Java Persistence API (JPA): 2 Inheritance and Querying

Start my 1-month free trial

Create queries with Criteria API

- [Instructor] Let's learn how to create queries using the Criteria API and review the parallels between JPQL and the criteria API. Let's look at an example that shows a JPQL query as a criteria query. Let's go back to our use case to retrieve all tickets. Let's break the code down line by line. This query demonstrates the basic steps to create a criteria query. So, in this case, we use an entityManager instance to create a CriteriaBuilder object. Then, we use the CriteriaBuilder instance to create a query object. This query object's attributes will be modified with the details of the query. The query will return instances of the ticket entity. Next, we set the query root by calling the from method on the criteria query object. This sets the from clause of the query. Next, we specify what the type of the query result will be by calling the select method of the criteria query object. When passing in the query root, this sets the select clause of the query. Next, we prepare the query…

Contents