Maintaining List Order in Jpa
If you want to maintain the order of a list of objects retrieve from the database using hibernate/jpa, use the @OrderColumn annotation. An additional column will be created in the database (if ddl is set to update) that will keep track of the order/position of an item in a list.
CAVEAT: If you change the position of an item in a list OR delete an item besides the last one, it will cause a multi update across the list to update all other positions. Be aware of this intensive operation for large lists.
@Entity
@Table(name = "students")
class Student {
@OneToMany(mappedBy = "student")
@OrderColumn(name = "assignment_order_id")
private List<Assignment> assignments;
}