
How to display record in order using Criteria API?

Hibernate Criteria API provides an easy way of building dynamic query on the persistence database.
The hibernate criteria API is very Simplified API for fetching data from Criterion objects. The criteria API is an alternative of HQL (Hibernate Query Language) queries. It is more powerful and flexible for writing tricky criteria functions and dynamic queries.
You can easily order your record by using Criteria API either in ascending order or in descending order.
Example- In ascending order-
Session session = sessionFactory.openSession();
Criteria criteria = session.createCriteria(Employee.class);
criteria.addOrder(Order.asc("name"));
In descending order-
Criteria criteria = session.createCriteria(Employee.class);
criteria.addOrder(Order.desc("name"));
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.