BoxLang: Our new JVM Dynamic Language made by Ortus! Check it out: https://www.boxlang.io

JPA Criteria Queries Support

Description

Goal

Hibernate has deprecated the original criteria API. The new approach is using the JPA standard for criteria queries. CBORM needs to implement the new approach in a more fluent and dynamic matter.

Resources

 

If you use JPA’s Criteria API together with its metamodel, you no longer need to reference entity attributes by their name. You can use the attributes of the generated metamodel classes instead. That enables you to define your query in a typesafe way. It not only makes the definition of your query easier; it also makes a refactoring of your entity classes a lot easier.

JPA’s CriteriaBuilder provides a method to define bind parameters, which you can use to create your WHERE clause and to set the corresponding values before you execute the queries. In contrast to inline parameters, bind parameters avoid SQL injection vulnerabilities and enable your database to cache the execution plan of your query.

Java Basics

CriteriaBuilder builder = session.getCriteriaBuilder(); CriteriaQuery<Employee> query = builder.createQuery(Employee.class); Root<Employee> root = query.from(Employee.class); query.select(root); Query<Employee> q=session.createQuery(query); List<Employee> employees=q.getResultList();
CriteriaBuilder builder = session.getCriteriaBuilder(); CriteriaQuery<Department> query = builder.createQuery(Department.class); Root<Department> root = query.from(Department.class); query.select(root).where(builder.equal(root.get("id"), 1l)); Query<Department> q=session.createQuery(query); Department department=q.getSingleResult(); System.out.println(department.getName());
CriteriaBuilder builder = session.getCriteriaBuilder(); CriteriaQuery<String> query = builder.createQuery(String.class); Root<Employee> root = query.from(Employee.class); query.select(root.get("name")); Query<String> q=session.createQuery(query); List<String> list=q.getResultList();
// Count number of employees CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class); Root<Employee> root = criteriaQuery.from(Employee.class); criteriaQuery.select(builder.count(root)); Query<Long> query = session.createQuery(criteriaQuery); long count = query.getSingleResult(); System.out.println("Count = " + count); // Get max salary CriteriaQuery<Integer> criteriaQuery2 = builder.createQuery(Integer.class); Root<Employee> root2 = criteriaQuery2.from(Employee.class); criteriaQuery2.select(builder.max(root2.get("salary"))); Query<Integer> query2 = session.createQuery(criteriaQuery2); int maxSalary = query2.getSingleResult(); System.out.println("Max Salary = " + maxSalary); // Get Average Salary CriteriaQuery<Double> criteriaQuery3 = builder.createQuery(Double.class); Root<Employee> root3 = criteriaQuery3.from(Employee.class); criteriaQuery3.select(builder.avg(root3.get("salary"))); Query<Double> query3 = session.createQuery(criteriaQuery3); double avgSalary = query3.getSingleResult(); System.out.println("Average Salary = " + avgSalary); // Count distinct employees CriteriaQuery<Long> criteriaQuery4 = builder.createQuery(Long.class); Root<Employee> root4 = criteriaQuery4.from(Employee.class); criteriaQuery4.select(builder.countDistinct(root4)); Query<Long> query4 = session.createQuery(criteriaQuery4); long distinct = query4.getSingleResult(); System.out.println("Distinct count = " + distinct); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Author> cq = cb.createQuery(Author.class); Root<Author> root = cq.from(Author.class); SetJoin<Author, Book> books = root.join(Author_.books); ParameterExpression<String> paramTitle = cb.parameter(String.class); cq.where(cb.like(books.get(Book_.title), paramTitle)); TypedQuery<Author> q = em.createQuery(cq); q.setParameter(paramTitle, "%Hibernate%"); List<Author> authors = q.getResultList();

Activity

Show:
Pinned fields
Click on the next to a field label to start pinning.

Details

Assignee

Reporter

Components

Priority

Sentry

Created January 13, 2022 at 3:27 PM
Updated January 14, 2022 at 4:48 PM

Flag notifications