We want a collection field belonging to an entity object to be sorted.
There are two possible solutions:
- If you want the collection to be sorted when loaded, but not necessarily keep that sorting order all the time, use the OrderBy annotation where you set the actual ORDER BY SQL clause. In that case, the collection can be of any type (e.g. Collection).
If you want the collection to be sorted at all times, make it of type SortedSet or SortedMap and set the Sort annotation.
- If the objects in the collection implements Comparable set type to SortType.NATURAL.
- Otherwise set it to SortType.COMPARATOR and specify your own Comparator class in the comparator attribute.
- There exists an annotation with name OrderBy in both the javax.persistence package as well as the org.hibernate.annotations package. The former should be used.