Pinned fields
Click on the next to a field label to start pinning.
Details
Details
Details
Sentry
Sentry
Sentry
Created January 13, 2022 at 3:27 PM
Updated January 14, 2022 at 4:48 PM
BoxLang: Our new JVM Dynamic Language made by Ortus! Check it out: https://www.boxlang.io
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
https://www.baeldung.com/hibernate-criteria-queries
https://www.boraji.com/hibernate-5-criteria-query-example
https://thorben-janssen.com/migration-criteria-api/
https://jpastreamer.org/
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();