Prev Next

Hibernate / Hibernate Interview Questions III

How do I order the selected entity when using CriteriaQuery?

You can define an ORDER BY clause with the orderBy method of the CriteriaQuery interface and the asc or desc method of the CriteriaBuilder interface.

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Employee> cq = cb.createQuery(Employee.class);
Root<Book> root = cq.from(Employee.class);
cq.orderBy(cb.asc(root.get(Employee_.firstName)));
 
// Execute query with pagination
List<Employee> emplList = em.createQuery(cq).getResultList();

More Related questions...

Show more question and Answers...


Comments & Discussions