criteria order by clause

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

We can order the results of a query according to some condition. Criteria order by clause enables us to order the results of criteria query.

Criteria order by clause:

We have seen how to order the results using JPQL in jpa orderby clause article. The query to order teachers by salary used was

Criteria order by clause code is as below.

Here, orderBy() method does the ordering. This is straight from javadoc.

Specify the ordering expressions that are used to order the query results. Replaces the previous ordering expressions, if any. If no ordering expressions are specified, the previous ordering, if any, is simply removed, and results will be returned in no particular order. The order of the ordering expressions in the list determines the precedence, whereby the first element in the list has highest precedence.

We can execute the query formed below as follows

This prints out the results

Prasad 20000
Pranil 20000
Sushsant 40000

I hope this example helped understand criteria order by clause.

References for further study :

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Leave a Reply

Your email address will not be published. Required fields are marked *