If you want to dynamically sort a set, create a List from it and pass it to Collections.sort, or create a TreeSet from it with the correct Comparator. There are many other alternatives, too. If you use a SortedSet in your mapping file, the order is fixed, but can be changed in another Set.
Alternatively, use a Set (unsorted) and make use of order by in your HQL query. Hibernate backs Sets with PersistentSet, not HashSet, so doesn't necessarily sort by hashCode. If you use Criteria, use addOrder.
Re: point 3: why would the hash code change each time you load a set or list? Are you using java's default hashCode/equals for persistent objects? This is a very bad idea, seeing as you're using sets/lists: you should use a business key (not a DB key) for this purpose. Read section "Implementing equals() and hashCode()" in chapter "Persistent Classes" of the hib ref docs.
|