JPA Criteria Queries | Code Factory

1. Overview

2. Maven Dependencies


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);;

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"itemPrice"), 1000));"itemPrice"), 1000));"itemName"), "%chair%"));"itemPrice"), 100, 200));"itemDescription")));"itemDescription")));
Predicate[] predicates = new Predicate[2];
predicates[0] = cb.isNull(root.get("itemDescription"));
predicates[1] ="itemName"), "chair%");;
Predicate greaterThanPrice ="itemPrice"), 1000);
Predicate chairItems ="itemName"), "Chair%");, chairItems));, chairItems));

3.2. Sorting


3.3. Projections, Aggregates And Grouping Functions

CriteriaQuery<Long> cr = cb.createQuery(Long.class);
Root<Item> root = cr.from(Item.class);;
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);"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();

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();




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


Experience with Spring batch and docker