Home » JPA Criteria ORDER BY Clause

JPA Criteria ORDER BY Clause

by Online Tutorials Library

JPA ORDER BY Clause

The ORDER BY clause is used to sort the data and arrange them either in ascending or descending order. The CriteriaQuery interface provides orderBy() method to define the type of ordering.

ORDER BY Example

Here, we will perform several ORDER BY operations on student table. Let us assume the table contains the following records: –

JPA ORDER BY Clause

Now, follow the below steps to perform operations: –

  • Create an entity class. Here, we created StudentEntity.java under com.tutoraspire.jpa package. This class contains three attributes s_id, s_name, s_age with all the required annotations.

StudentEntity.java

  • Now, map the entity class and other databases confiuguration in Persistence.xml file.

Persistence.xml

  • Once, we have created the basic entity class and mapped the configuration into persistence.xml file, we can perform the different types of select operations in the following ways: –

Sorting in ascending order

Asc.java

output:

JPA ORDER BY Clause

Sorting in descending order

Desc.java

output:

JPA ORDER BY Clause

You may also like