hibernate sql 查询多表

public List<VirtualShopRuleForm> queryDistinct(
VirtualShopRuleForm formbean, boolean ispager) {
StringBuffer hsql = new StringBuffer(
" select distinct mc.MC_CORPORATE_NO,mc.MC_CORPORATE_NAME from MERCHANT_CORPORATE mc,T_COMMODITY_VSHOP_MATCHRULE r,T_COMMODITY_VSHOP s ");
hsql.append(" where  s.VALID_STATUS='Y' and  r.VSHOP_ID=s.VSHOP_ID ");
hsql.append(" and mc.MC_CORPORATE_NO=r.CORPORATE_NO ");

Session session = null;
List<VirtualShopRuleForm> shoplist = new ArrayList<VirtualShopRuleForm>();
List<Object[]> list = new ArrayList<Object[]>();
try {
session = openSession();
if (formbean != null) {

if (StringUtils.isNotEmpty(formbean.getTempcorporateNo())) {
hsql.append(" and r.CORPORATE_NO='"
+ formbean.getTempcorporateNo().split("-")[0]
+ "'");
}

// 非 * 终门店,* 表示适用所有门店
if (StringUtils.isNotEmpty(formbean.getTempmerchantNo())) {
hsql.append(" and (r.MERCHANT_NO= '"
+ formbean.getTempmerchantNo().split("-")[0]
+ "' or r.MERCHANT_NO = '*') ");
}

// 非 * 终端号,* 表示适用所有终端
if (StringUtils.isNotEmpty(formbean.getTempterminalNo())) {
hsql.append(" and (r.TERMINAL_NO = '"
+ formbean.getTempterminalNo()
+ "' or r.TERMINAL_NO = '*') ");
}

// 所属发卡方,非'CUL' 用户 不能查看其他公司的受理规则
if (StringUtils.isNotEmpty(formbean.getTempissuerId())) {
// issuerId
hsql.append(" and s.ISSUER_ID='"
+ formbean.getTempissuerId() + "' ");

}

if (StringUtils.isNotEmpty(formbean.getTempcardType_level())) {
hsql.append(" and r.CARD_TYPE='"
+ formbean.getTempcardType_level().split("-")[0]
+ "'");
}
if (StringUtils.isNotEmpty(formbean.getTempcardType_level())) {
hsql.append(" and r.CARD_LEVEL='"
+ formbean.getTempcardType_level().split("-")[1]
+ "'");
}

if (StringUtils.isNotEmpty(formbean.getTempcardType())) {
hsql.append(" and r.CARD_TYPE='"
+ formbean.getTempcardType() + "'");
}
if (StringUtils.isNotEmpty(formbean.getTempcardLevel())) {
hsql.append(" and r.CARD_LEVEL='"
+ formbean.getTempcardLevel() + "'");
}

if (StringUtils.isNotEmpty(formbean.getTempvshopId())) {
hsql.append(" and r.VSHOP_ID='"
+ formbean.getTempvshopId().split("-")[0] + "'");
}

}
System.out.println("HSQL===虚拟店铺受理规则查询====" + hsql.toString());

list = session.createSQLQuery(hsql.toString()).list();
VirtualShopRuleForm vo = null;
if (list.size() > 0) {
for (Object[] os : list) {
vo = new VirtualShopRuleForm();
String corpno = (os[0] != null && os[0] != "") ? os[0]
.toString() : null;
String corpname = (os[1] != null && os[1] != "") ? os[1]
.toString() : null;
vo.setTempcorporateNo(corpno);
vo.setTempcorporateName(corpname);
shoplist.add(vo);
}

}

} catch (Exception e) {
logger.error("ERROR: ", e);
e.printStackTrace();
} finally {
SessionFactoryUtils.closeSession(session);
}
return shoplist;
}

你可能感兴趣的:(Hibernate)