Criteria API in JPA

The JPA Criteria API allows the developers to create queries using the fluent API approach. It comprises the set of classes and interfaces to define the query elements such as the selections, conditions, joins, and ordering criteria.

Steps to understand how to use the Criteria API

Below are the steps to know the working of Criteria API in JPA.

Step 1: Building the Criteria Query

We can begin by creating a CriteriaBuilder instance which acts as the factory for the query elements. The below code snippet demonstrates the initialization of the CriteriaBuilder.

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

Step 2: Creating the CriteriaQuery

We can create the CriteriaQuery Object. It can be specifying the result type of the query. This step defines what type of object the query will be retrieve.

Example:

CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);

Step 3: Defining the Query Elements

We can proceed to define the query elements such as the selections, conditions, joins, and ordering criteria using CriteriaQuery’s methods. For instance, to select the specific attributes.

Root<Employee> root = criteriaQuery.from(Employee.class);
criteriaQuery.select(root.get("name"));

Step 4: Executing the Query

Finally, we can execute the query using EntityManager’s createQuery method of the JPA application. The below code snippet for the demonstration of the topic.

TypedQuery<String> typedQuery = entityManager.createQuery(criteriaQuery);
List<String> names = typedQuery.getResultList();

Key Components

There are some futher subpoints that we need to know the concept of Criteria API of JPA.

Predicates and Expressions:

  • Predicates: Criteria API can provides the Predicate interface to the represent conditions in the query. these can be combined using the logical operators like AND, OR etc.
  • Expressions: It can be represents the values, paths or the functions in the query. CriteriaBuilder can provides the methods to the create expressions for various purposes.

Joins:

Criteria API can supports the different types of the joins like inner join, left join and right join. Developers can construct the join conditions dynamically based on the requirements.

CriteriaQuery Functions:

Criteria API can offers the functions to the perform the operations like aggregation, mathematical calculations, date/time manipulations etc.., directly into the query.

JPA – Criteria API

In Java, JPA is defined as Java Persistence API, and the Criteria API provides a powerful tool for constructing the queries dynamically at the runtime. Unlike the JPQL (Java Persistence Query Language) which uses the strings to represent the queries programmatically.

Similar Reads

Criteria API in JPA

The JPA Criteria API allows the developers to create queries using the fluent API approach. It comprises the set of classes and interfaces to define the query elements such as the selections, conditions, joins, and ordering criteria....

Implementation of Criteria API in JPA

We can develop the simple JPA application that can demonstrate the Criteria API clause of the application....