当前位置:编程学习 > JAVA >>

Hbiernate关联排序问题

使用场景:
假设有两张表请求信息、账户表,它们之间是一对多的关系。对应的java类分别为Sfcx_RequestInfo和Sfcx_Zhxx。Sfcx_RequestInfo有一个Set属性 sfcx_Zhxxs,需要对Sfcx_Zhxx按账户信息的查询序号(ccxh)进行排序。
解决方案:
 
1:配置账户信息对比辅助类。也可定义内部类或者写到Sfcx_RequestInfo中。实现Comparator接口。重写compare方法。在此方法中指定配需规则。
 
package com.levelappro.gbism.app.sfcx.model;  
  
import java.util.Comparator;  
  
public class ComparatorZhxx implements Comparator<Sfcx_Zhxx> {  
  
    public int compare(Sfcx_Zhxx z1, Sfcx_Zhxx z2) {  
            if(z1.getCcxh().compareTo(z2.getCcxh())>0)   
            return 1;   
            if(z1.getCcxh().compareTo(z2.getCcxh())<0)   
            return -1;   
            return 0;   
        }   
  
}  

 

 
 
 
2:在一的一方(Sfcx_RequestInfo)定义一个新的的get方法getSfcx_ZhxxsTreeSet用来去排序后的TreeSet.
/** 
     * 获取有序的账户信息集合,根据查询序号排序 
     * @return 
     */  
    public TreeSet<Sfcx_Zhxx> getSfcx_ZhxxsTreeSet() {  
        TreeSet<Sfcx_Zhxx> zhxxTreeSet = new TreeSet<Sfcx_Zhxx>(new ComparatorZhxx());  
        zhxxTreeSet.addAll(sfcx_Zhxxs);  
        return zhxxTreeSet;  
    }  

 

 
3:在获取账户信息set的时候调用getSfcx_ZhxxsTreeSet获取类型为TreeSet的集合。
 
for(Sfcx_RequestInfo info :infos){  
            TreeSet<Sfcx_Zhxx> zhxxSet = info.getSfcx_ZhxxsTreeSet();  

 

 
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,