What is the difference between sorted and ordered collection in Hibernate?
What is the difference between sorted and ordered collection in Hibernate?
A sorted collection is sorting a collection by utilizing the sorting methods provided by the Java collections. The sorting is done in the memory of JVM that is running hibernate after the data being read from database using Java comparator. This is efficient way to sort when collection is small.
An ordered collection is sorting a collection by specifying order-by clause or using OrderBy annotation when retrieval. This is more efficient when the collection is very large. With ordered collection you are saving process time allowing RDBMS sorting data in a fast-way, rather than ordering data in Java once received. Furthermore order-by clause or OrderBy annotation does not force you to use SortedSet or SortedMap collection. You can use any collection like HashMap, HashSet, or even a Bag, because hibernate will use internally a LinkedHashMap, LinkedHashSet or ArrayList respectively.