JPA Criteria Queries | Code Factory

1. Overview

2. Maven Dependencies

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.2.Final</version>
</dependency>

3. Simple Example Using Criteria

public class Item implements Serializable {

private Integer itemId;
private String itemName;
private String itemDescription;
private Integer itemPrice;

// setters and getters
}
Session session = HibernateUtil.getHibernateSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<Item> cr = cb.createQuery(Item.class);
Root<Item> root = cr.from(Item.class);
cr.select(root);

Query<Item> query = session.createQuery(cr);
List<Item> results = query.getResultList();
  1. Create an instance of Session from the SessionFactory object
  2. Create an instance of CriteriaBuilder by calling the getCriteriaBuilder() method
  3. Create an instance of CriteriaQuery by calling the CriteriaBuilder createQuery() method
  4. Create an instance of Query by calling the Session createQuery() method
  5. Call the getResultList() method of the query object which gives us the results

3.1. Using Expressions

cr.select(root).where(cb.gt(root.get("itemPrice"), 1000));
cr.select(root).where(cb.lt(root.get("itemPrice"), 1000));
cr.select(root).where(cb.like(root.get("itemName"), "%chair%"));
cr.select(root).where(cb.between(root.get("itemPrice"), 100, 200));
cr.select(root).where(cb.isNull(root.get("itemDescription")));
cr.select(root).where(cb.isNotNull(root.get("itemDescription")));
Predicate[] predicates = new Predicate[2];
predicates[0] = cb.isNull(root.get("itemDescription"));
predicates[1] = cb.like(root.get("itemName"), "chair%");
cr.select(root).where(predicates);
Predicate greaterThanPrice = cb.gt(root.get("itemPrice"), 1000);
Predicate chairItems = cb.like(root.get("itemName"), "Chair%");
cr.select(root).where(cb.or(greaterThanPrice, chairItems));
cr.select(root).where(cb.and(greaterThanPrice, chairItems));

3.2. Sorting

cr.orderBy(
cb.asc(root.get("itemName")),
cb.desc(root.get("itemPrice")));

3.3. Projections, Aggregates And Grouping Functions

CriteriaQuery<Long> cr = cb.createQuery(Long.class);
Root<Item> root = cr.from(Item.class);
cr.select(cb.count(root));
Query<Long> query = session.createQuery(cr);
List<Long> itemProjected = query.getResultList();
CriteriaQuery<Double> cr = cb.createQuery(Double.class);
Root<Item> root = cr.from(Item.class);
cr.select(cb.avg(root.get("itemPrice")));
Query<Double> query = session.createQuery(cr);
List avgItemPriceList = query.getResultList();

3.4. CriteriaUpdate

CriteriaUpdate<Item> criteriaUpdate = cb.createCriteriaUpdate(Item.class);
Root<Item> root = criteriaUpdate.from(Item.class);
criteriaUpdate.set("itemPrice", newPrice);
criteriaUpdate.where(cb.equal(root.get("itemPrice"), oldPrice));

Transaction transaction = session.beginTransaction();
session.createQuery(criteriaUpdate).executeUpdate();
transaction.commit();

3.5. CriteriaDelete

CriteriaDelete<Item> criteriaDelete = cb.createCriteriaDelete(Item.class);
Root<Item> root = criteriaDelete.from(Item.class);
criteriaDelete.where(cb.greaterThan(root.get("itemPrice"), targetPrice));

Transaction transaction = session.beginTransaction();
session.createQuery(criteriaDelete).executeUpdate();
transaction.commit();

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Notice: Coinigy V1 API to be Disabled

Azure DevOps Tips: “Each” Loops

Exam questions about other AWS Services for the Solutions Architect Associate Certification

AWS Solutions Architect Associate Course. Extra services.

I Can Do Things That Are Hard: How Learning to Code Changed My Life

CS373 Fall 2021 Blog 4

Review of Udemy SQL course, “ The Complete SQL Bootcamp 2020: Go from Zero to Hero ”

Carbon v11 Accessibility

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Reactive Programming in Java-RxJava

Connecting local Java client with Elastic cloud (Elasticsearch on Cloud) on Azure/AWS

HOW TO USE PREPARED STATEMENT IN JDBC

Experience with Spring batch and docker