• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

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();



 

 

posted @ 2013-09-03 19:46  Class Xman  阅读(217)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3