加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何根据特定字段创建一组有序的对象?

发布时间:2020-12-15 05:13:01 所属栏目:Java 来源:网络整理
导读:我需要根据排序字段检索排序的对象列表;它们的集合类型是SortedSet,但代码抛出异常. 我还尝试添加@Sort注释,如 hibernate documentation的排序集合部分所述,但它似乎已被弃用! 例外 Caused by: org.hibernate.AnnotationException: A sorted collection mus
我需要根据排序字段检索排序的对象列表;它们的集合类型是SortedSet,但代码抛出异常.
我还尝试添加@Sort注释,如 hibernate documentation的排序集合部分所述,但它似乎已被弃用!

例外

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

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

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

解决方法

您需要指定要放在ORDER BY子句中的列名

尝试将此注释添加到SortedSet

@javax.persistence.OrderBy("sort")

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读