@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 Annotation
Example 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.