Hibernate - @OrderBy Annotation with Example Last Updated : 07 Jun, 2023 Comments Improve Suggest changes Like Article Like Report @OrderBy annotation is used in Hibernate to specify the ordering of the elements in the collection valued property of an entity class. It is used to define the order of the elements which should be displayed. We can order the data within the entity using a specific parameter in ascending or descending order. Examples of @OrderBy AnnotationExample 1: Java // on the below line we are creating an entity for // department class. @Entity public class Department { // on the below line we are creating a variable for // department id. @Id @GeneratedValue private Long departmentID; // on the below line we are creating a string variable // for department name. private String departmentName; // on the below line we are creating a variable for // employees list and ordering it by employee name in // ascending order. @OneToMany(mappedBy = "department") @OrderBy("employeeName ASC") private List<Employee> employees; } // on the below line we are creating an Employee class. @Entity public class Employee { // on the below line we are creating an employee id // variable. @Id @GeneratedValue private Long empID; // on the below line we are creating an employee name // variable. private String employeeName; // on the below line we are creating a department // variable and joining it with column using department // id. @ManyToOne @JoinColumn(name = "departmentID") private Department department; } Explanation: In the above example, the Department entity has a one-to-many relationship with the Employee entity. The @OrderBy annotation is applied to the employee's collection in the Department entity. It specifies that the associated Employee entities should be ordered by the employee name field in ascending order. When the Department entity is being loaded from the Database, then the associated employee entity will be called and ordered based on the employee names in ascending order. The @OrderBy annotation allows you to control the order of elements in the collection-valued property which provides flexibility in how elements are retrieved and displayed in the Spring application using Hibernate. Example 2: Java // on the below line we are creating an entity for Team // class. @Entity public class Team { // on the below line we are creating a variable for team // id. @Id @GeneratedValue private Long id; // on the below line we are creating a string variable // for team name. private String teamName; // on the below line we are creating a variable for // players list and ordering it by player name in // ascending order. @OneToMany(mappedBy = "team") @OrderBy("playerName ASC") private List<Player> players; } // on the below line we are creating a Player class. @Entity public class Player { // on the below line we are creating a player id // variable. @Id @GeneratedValue private Long playerID; // on the below line we are creating an player name // variable. private String playerName; // on the below line we are creating a team variable and // joining it with column using id. @ManyToOne @JoinColumn(name = "id") private Team team; } Explanation: In the above example the Team entity has a one-to-many relationship with the Player entity. The @OrderBy annotation is applied to the player's collection in the Team entity. It specifies that the associated player entities should be ordered by the player name field in ascending order. When the Team entity is being loaded from the Database, the associated player entity will be called and ordered based on the player names in ascending order. The @OrderBy annotation allows you to control the order of elements in the collection-valued property which provides flexibility in how elements are retrieved and displayed in the Spring application using Hibernate. Comment More infoAdvertise with us Next Article Hibernate Example without IDE C chaitanyamunje Follow Improve Article Tags : Java Java-Hibernate Java-Spring-Data-JPA Practice Tags : Java Similar Reads Hibernate - @Version Annotation with Example @Version annotation is used to specify the version number for a specific entity. Version Number provided using @Version annotation is used to prevent concurrent modification to an entity. When an entity is being updated, the version number is also incremented. If another transaction tries to update 2 min read Hibernate - @Transient Annotation with Example @Transient annotation in Hibernate is used to mark a property or field in an entity class as transient. This means that the field or property marked as transient should be excluded when the data persists in the database. Wherever an entity is mapped to a database table, by default all the non-transi 4 min read Hibernate Example without IDE Hibernate is a powerful tool used to build applications that need to interact with a database. It is a Java framework that implements the ORM(Object Relational Mapping) technique. What is ORM? ORM stands for Object Relational Mapping. It is a technique that is used to make Java objects persistent b 3 min read Hibernate - @Lob Annotation Many times there is a scenario while storing the data in the database that we have to store images or files within our database for a specific entity. In that case, we can use @Lob annotation which will help us to map large binary objects or large character objects to a specific entity in our databa 3 min read Hibernate - @Lob Annotation Many times there is a scenario while storing the data in the database that we have to store images or files within our database for a specific entity. In that case, we can use @Lob annotation which will help us to map large binary objects or large character objects to a specific entity in our databa 3 min read Hibernate - @OneToOne Annotation @OnetoOne annotation in Hibernate is used to create a one-to-one association between two entities. The one-to-one annotation indicates that one instance of an entity is associated with only one instance of the other entity. When we annotate a field or method with @Onetoone annotation then Hibernate 4 min read Like