How to sort using Criteria in Hibernate

In this post, we will see How to sort using Criteria in Hibernate. We will see Criteria addOrder() example Using Spring Boot. We will fetch record from the database and we will sort on the basis of some field(let’s say studentName). Criteria interface has addOrder() method which used for sorting purpose. At the end of the post, we will also see how to customized order class for sorting purpose.

How to sort using Criteria in Hibernate

public Criteria addOrder(Order order); 

Order is a class which contains two static methods asc(String propertyName) and desc(String propertyName) which is mainly used for sorting.

Let’s see how to use addOrder() method for sorting.

 

Let’s see a complete example of sort using Criteria in Hibernate using Spring Boot and Oracle. Before going ahead let’s see what are we going to do. We will have one entity Student.java which have three fields(id, studentName and rollNumber). First, we will save some student records in the database using REST API and then we will fetch all record using Criteria which would be in sorted order.

 

First, we will save record using the below rest API.

http://localhost:9091/student/save

Request Data-

 

Response data for save call.

 

REST API to fetch all students.

http://localhost:9091/student/allstudents – GET Operation

Response data which is sorted on the base of  studentName.

 

Generated Query while the get operation.

Hibernate: select this_.id as id1_0_0_, this_.roll_number as roll_number2_0_0_, this_.student_name as student_name3_0_0_ from student this_ order by this_.student_name asc

Criteria addOrder() example in Hibernate Using Spring Boot.

Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’ click on next.  Fill all details(GroupId – criteriasortingexample, ArtifactId – criteriasortingexamplename – criteriasortingexample) and click on finish. Keep packaging as the jar.

Modify pom.xml

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

If you see any error for oracle dependency then follow these steps.

Directory structure –

How to sort using Criteria in Hibernate

 

 

 

Student.java

 

StudentController.java

StudentRepository.java – interface

StudentService.java – interface

 

StudentServiceImpl.java

SpringMain.java

 

JpaConfig.java

application.properties

Let’s run the SpringMain class(run as java application).

Perform save operation first using below REST API(Use request data to perform save operation which has been given in the beginning of the post).

http://localhost:9091/student/save  – POST Operation

Expected response data –

How to sort using Criteria in Hibernate

Perform GET operation using below REST API.

http://localhost:9091/student/allstudents

In response, we should have sorted data basis of student name as below.

How to sort using Criteria in Hibernate

 

Custom Order.

We can define our own Custom Order for sorting. Let’s see a simple use case where you may need a custom order. Consider the above example, we want to sort on the basis of length of studentName. That means if we sort in ascending order then the studentName which contains less character, should come first.

We want query something like below.

select * from student order by LENGTH(student_name) asc;

select this_.id as id1_0_0_, this_.roll_number as roll_number2_0_0_, this_.student_name as student_name3_0_0_ from student this_ order by LENGTH(student_name) asc

We can define custom class extending Order class and override toSqlString() method.

See here complete example which explains how to create custom order in hibernate.

That’s all about How to sort using Criteria in Hibernate.

You may like –

Criteria interface docs.

 

Top