title: How to have a sorted set of objects based on a specific field?
body: <p>I need to retrieve a sorted list of objects based on their <code>sort field</code>; Their collection type is <code>SortedSet</code> but the code throws following exception.
  I also tried to add <code>@Sort</code> annotation as explained in sorted collection section of <a href="https://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/collections.html" rel="noreferrer">hibernate documentation</a> but it seems like it is deprecated!</p>
  
  <p><strong>Exception</strong> </p>
  
  <pre><code>Caused by: org.hibernate.AnnotationException: A sorted collection must define and ordering or sorting
  </code></pre>
  
  <p><strong>Code</strong></p>
  
  <pre><code>SortedSet&lt;Offer&gt; offers = new TreeSet&lt;Offer&gt;();
  </code></pre>
  
  <p><strong>Classes</strong></p>
  
  <pre><code>public class Person {
   @Id
   long id;
   @OneToMany 
   //@OrderBy("sort ASC") &lt;&lt;&lt; If I use this just one offer will be in the collection 
   SortedSet&lt;Offer&gt; offers = new TreeSet&lt;Offer&gt;();
   ...
  }
  
  public class Offer implements Comparable&lt;Offer&gt;{
  
   @Id
   long id;
   String name;
   short sort;
   @Override
   public int compareTo(Offer o) {
      return sort - o.sort;
   }
  }
  </code></pre>