从制造到创造
软件工程师成长之路
posts - 292,  comments - 96,  trackbacks - 0
通过T-SQL,我们左联(内联、右连类似)的查询语法如下:

-- 通過 InsuredIn 查詢 累計類別 

select * from ris.re_master as rm left join ris.re_detail as rd on rm.REINSURANCE_NO=rd.REINSURANCE_NO
    
where rd.INSURED_ID='A120670116'
    
order by rm.POLICY_NO asc, rm.POLICY_SEQNO asc
    ;


但是使用 HSQL 查询数据库时,我们要改为:

from ReMaster as rm left join rm.reDetails as rd 


Hibernate 会自动找相同的键,不用on,而且关联表要写成:rm.reDetails


/**
 * description: 通過被保人號碼查詢再保明細
 * 
 * 
@param insuredId
 *            被保人身份證號 String
 * 
@return List 結果列表
 * 
@throws DbAccessException
 *             數據庫異常
 
*/
public List selectReMasterDetail(String insuredId) 
    
throws DbAccessException {
    
if (DEBUGLOG.isDebugEnabled()) {
        DEBUGLOG.debug(
"[ReDetailDao]"
                
+ "[Function:selectReMasterDetail][Begin]");
    }
    StringBuffer hqlRd 
= new StringBuffer();

    hqlRd.append(
" from ReMaster as rm left join rm.reDetails as rd " +
            
"where 1=1");

    
if (insuredId != null) {
        hqlRd.append(
" and rd.insuredId = '" + insuredId + "'");
    }
    
    hqlRd.append(
" order by rm.policyNo asc, rm.policySeqno asc");

    
if (DEBUGLOG.isDebugEnabled()) {
        DEBUGLOG.debug(
"[ReDetailDao][Function:selectReMasterDetail][End]");
    }
    
    
return this.createQuery(hqlRd.toString());
}

在Service层进行调用:
// 通過 InsuredId 查詢ReMaster檔和ReDetail檔
rmdList = rdDao.selectReMasterDetail(iqVo.getInsuredId());

取List中的对象时用对象数组接,因为返回的是两个对象:



    Object[] obj = null;
    ReDetail rd 
= null;
    ReMaster rm 
= null;
    
int rmdSize = 0;
    
if (rmdList != null) {
        rmdSize 
= rmdList.size();
    }
    
for (int rmdIndex = 0; rmdIndex < rmdSize; rmdIndex++) {
        obj 
= new Object[2];
        obj 
= (Object[])rmdList.get(rmdIndex);
    
        rm 
= new ReMaster(); // 用於存儲公司別信息
        rd = new ReDetail();
        rm 
= (ReMaster) (obj[0]);
        rd 
= (ReDetail) (obj[1]);

这样就可以处理得到的ReMaster对象和ReDetail对象了。
posted on 2007-07-18 11:25 CoderDream 阅读(2453) 评论(1)  编辑  收藏 所属分类: 经验点滴

FeedBack:
# re: HSQL 中的左联查询
2009-04-25 10:19 | meidao
正好用到这里,学习了,3Q  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

<2007年7月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(9)

我参与的团队

随笔分类(245)

随笔档案(239)

文章分类(3)

文章档案(3)

收藏夹(576)

友情链接

搜索

  •  

积分与排名

  • 积分 - 454034
  • 排名 - 115

最新评论

阅读排行榜

评论排行榜