通过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 阅读(2472) 
评论(1)  编辑  收藏  所属分类: 
经验点滴