Hibernate如何完成查詢結果的排序

來源:互聯網
上載者:User

 1、普通的查詢排序,直接寫HQL語句即可,即加上order by column

2、一對多裡面對set的排序,建議採用以下辦法:

首先,將POJO中的set成員的類型改為TreeSet類型,因為TreeSet是實現了SortedSet的可排序集合類。

private Set labproductflowinfos = new TreeSet(new FlowInfoComparator());//為TreeSet提供一個自訂的比較子

然後修改設定檔,為<set>添加sort屬性,屬性取值可以為一個自訂的比較子。如下:

public class FlowInfoComparator implements Comparator {

 public int compare(Object o1, Object o2) {
  if(o1 instanceof Labproductflowinfo &&
    o2 instanceof Labproductflowinfo){
   Integer s1 = ((Labproductflowinfo)o1).getSequenceNum();
   Integer s2 = ((Labproductflowinfo)o2).getSequenceNum();
   return s1.intValue() - s2.intValue();
  }
  return 0;
 }}

<set name="labproductflowinfos" inverse="true"
         cascade="all-delete-orphan" lazy="false" sort="com.zsc.hibernate.FlowInfoComparator">

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.