Hibernate Supports sorted collection mapping, you can specify your comparator to do the sorting for you.
From the docs
Hibernate supports collections implementing java.util.SortedMap and
java.util.SortedSet. You must specify a comparator in the mapping
file:
<set name="aliases"
table="person_aliases"
sort="natural">
<key column="person"/>
<element column="name" type="string"/>
</set>
<map name="holidays" sort="my.custom.HolidayComparator">
<key column="year_id"/>
<map-key column="hol_name" type="string"/>
<element column="hol_date" type="date"/>
</map>
Allowed values of the sort attribute are unsorted, natural and the
name of a class implementing java.util.Comparator.
Sorted collections actually behave like java.util.TreeSet
or
java.util.TreeMap
.
Edit Annotation From Docs, check
3.4.6. Collection related annotations
@Sort(type = SortType.COMPARATOR, comparator = TicketComparator.class)