此内容没有您所选择的语言版本。
7.3. Advanced collection mappings
7.3.1. Sorted collections 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Hibernate supports collections implementing
java.util.SortedMap and java.util.SortedSet. You must specify a comparator in the mapping file:
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.
If you want the database itself to order the collection elements, use the
order-by attribute of set, bag or map mappings. This solution is only available under JDK 1.4 or higher and is implemented using LinkedHashSet or LinkedHashMap. This performs the ordering in the SQL query and not in the memory.
Note
The value of the
order-by attribute is an SQL ordering, not an HQL ordering.
Associations can even be sorted by arbitrary criteria at runtime using a collection
filter():
sortedUsers = s.createFilter( group.getUsers(), "order by this.name" ).list();
sortedUsers = s.createFilter( group.getUsers(), "order by this.name" ).list();