Java – how to create an ordered set of objects based on specific fields?

I need to retrieve the sorted object list according to the sorting field; Their collection type is sortedset, but the code throws an exception

exception

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

code

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

class

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;
 }
}

Solution

You need to specify the column names to place in the order by clause

Try adding this comment to sortedset

@javax.persistence.OrderBy("sort")
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>