How can I have a sorted set of objects based on a specific field?

I need to get a sorted list of objects based on them sort field

; Their type is collection SortedSet

, but the code throws the following exception. I also tried adding annotation @Sort

as described in the sorted collection section of the hibernate documentation , but it looks like it is out of date!

An exception

Caused by: org.hibernate.AnnotationException: A sorted collection must define and ordering or sorting

      

code

SortedSet<Offer> offers = new TreeSet<Offer>();

      

Classes

public class Person {
 @Id
 long id;
 @OneToMany 
 //@OrderBy("sort ASC") <<< If I use this just one offer will be in the collection 
 SortedSet<Offer> offers = new TreeSet<Offer>();
 ...
}

public class Offer implements Comparable<Offer>{

 @Id
 long id;
 String name;
 short sort;
 @Override
 public int compareTo(Offer o) {
    return sort - o.sort;
 }
}

      

+3


source to share


1 answer


You need to provide the name of the column to be put in the ORDER BY clause

try to add this annotation to SortedSet



@javax.persistence.OrderBy("sort")

      

+7


source







All Articles