hibernate模糊查询id为long型的情况

 在hibernate中模糊查询long型数据的时候,网上例子比较好,这里写出来供大家讨论一下,这里要注意:
目前试过的方法中好象只有通过Restrictions.sqlRestriction的方式来进行
具体事例如下:


//此处主要是判断是否全区查询
if(searchCondition.getIsSelectedAllArea() != null && searchCondition.getIsSelectedAllArea().equals("on")){
    //criteria.createAlias("area", "o").add(Restrictions.like("o.id","%"+searchCondition.getAreaId().toString().substring(0, 4)+"%"));
    criteria.createAlias("area", "o").add(Restrictions.sqlRestriction("AREA_ID  like  (?)","%"+searchCondition.getAreaId().toString().substring(0, 4)+"%",Hibernate.STRING));
}else{
   if (searchCondition.getAreaId() != null && !searchCondition.getAreaId().equals("")) {
 criteria.createAlias("area", "o").add(Restrictions.eq("o.id", searchCondition.getAreaId()));
  }
}

附属上全部代码如下:
public List findBillQueryResult(SearchCondition searchCondition, String billTypeKey, SearchDateBean searchDateBean) {

  DetachedCriteria criteria;
  if (DOMSGlobalConstant.DICT_KEY_FOR_BILL_TYPE_TURNINBILL.equals(billTypeKey)) {
   criteria = DetachedCriteria.forClass(TurnBill.class);
  } else if (DOMSGlobalConstant.DICT_KEY_FOR_BILL_TYPE_MOVEBILL.equals(billTypeKey)) {
   criteria = DetachedCriteria.forClass(MoveBill.class);
  } else if (DOMSGlobalConstant.DICT_KEY_FOR_BILL_TYPE_OLDBILL.equals(billTypeKey)){
   criteria = DetachedCriteria.forClass(OldBill.class);
  }else if (DOMSGlobalConstant.DICT_KEY_FOR_BILL_TYPE_WASTERBILL.equals(billTypeKey)){
   criteria = DetachedCriteria.forClass(WasterBill.class);
  }else{
   criteria = DetachedCriteria.forClass(DrawBill.class);
  }
  if (searchCondition.getApplyAccount() != null && !searchCondition.getApplyAccount().equals("")) {
   criteria.createAlias("materialMan", "a").add(Restrictions.like("a.staffAccount", "%" + searchCondition.getApplyAccount() + "%").ignoreCase());
  }
  if (searchCondition.getAcountAccount() != null && !searchCondition.getAcountAccount().equals("")) {
   criteria.createAlias("accounter", "b").add(Restrictions.like("b.staffAccount", "%" + searchCondition.getAcountAccount() + "%").ignoreCase());
  }
  if (searchCondition.getApproveAccount() != null && !searchCondition.getApproveAccount().equals("")) {
   criteria.createAlias("leader", "c").add(Restrictions.like("c.staffAccount", "%" + searchCondition.getApproveAccount() + "%").ignoreCase());
  }
  if (searchCondition.getItemManagerAccount() != null && !searchCondition.getItemManagerAccount().equals("")) {
   criteria.createAlias("manager", "d").add(Restrictions.like("d.staffAccount", "%" + searchCondition.getItemManagerAccount() + "%").ignoreCase());
  }
  if (DOMSGlobalConstant.DICT_KEY_FOR_BILL_TYPE_MOVEBILL.equals(billTypeKey)) {
   if (searchCondition.getPlanAccount() != null && !searchCondition.getPlanAccount().equals("")) {
    criteria.createAlias("inDispatcher", "p").add(Restrictions.like("p.staffAccount", "%" + searchCondition.getPlanAccount() + "%").ignoreCase());
   }
   if (searchCondition.getPlanAccount() != null && !searchCondition.getPlanAccount().equals("")) {
    criteria.createAlias("outDispatcher", "q").add(Restrictions.like("q.staffAccount", "%" + searchCondition.getPlanAccount() + "%").ignoreCase());
   }
   if (searchCondition.getStoreAccount() != null && !searchCondition.getStoreAccount().equals("")) {
    criteria.createAlias("outStoreman", "r").add(Restrictions.like("r.staffAccount", "%" + searchCondition.getStoreAccount() + "%").ignoreCase());
   }
   if (searchCondition.getStoreAccount() != null && !searchCondition.getStoreAccount().equals("")) {
    criteria.createAlias("inStoreman", "s").add(Restrictions.like("s.staffAccount", "%" + searchCondition.getStoreAccount() + "%").ignoreCase());
   }
   if (searchCondition.getStoreHouseName() != null && !searchCondition.getStoreHouseName().equals("")) {
    criteria.createAlias("inStoreHouse", "t").add(Restrictions.like("t.storeHouseName", "%" + searchCondition.getStoreHouseName() + "%").ignoreCase());
   }
   if (searchCondition.getStoreHouseName() != null && !searchCondition.getStoreHouseName().equals("")) {
    criteria.createAlias("outStoreHouse", "u").add(Restrictions.like("u.storeHouseName", "%" + searchCondition.getStoreHouseName() + "%").ignoreCase());
   }
  } else {
   if (searchCondition.getPlanAccount() != null && !searchCondition.getPlanAccount().equals("")) {
    criteria.createAlias("dispatcher", "e").add(Restrictions.like("e.staffAccount", "%" + searchCondition.getPlanAccount() + "%").ignoreCase());
   }
   if (searchCondition.getStoreAccount() != null && !searchCondition.getStoreAccount().equals("")) {
    criteria.createAlias("storeman", "f").add(Restrictions.like("f.staffAccount", "%" + searchCondition.getStoreAccount() + "%").ignoreCase());
    if (searchCondition.getStoreHouseName() != null && !searchCondition.getStoreHouseName().equals("")) {
     criteria.createAlias("storeHouse", "n").add(Restrictions.like("n.storeHouseName", "%" + searchCondition.getStoreHouseName() + "%").ignoreCase());
    }
   }
  }
  if (searchCondition.getApplyCode() != null && !searchCondition.getApplyCode().equals("")) {
   criteria.add(Restrictions.like("applyCode", "%" + searchCondition.getApplyCode() + "%").ignoreCase());
  }
  if (searchCondition.getBillCode() != null && !searchCondition.getBillCode().equals("")) {
   criteria.add(Restrictions.like("billCode", "%" + searchCondition.getBillCode() + "%").ignoreCase());
  }
  if (searchCondition.getBillStatus() != null && !searchCondition.getBillStatus().equals("")) {
   criteria.createAlias("currentStatus", "h").add(Restrictions.eq("h.id", searchCondition.getBillStatus()));
  }
  if (searchCondition.getWoFlag() != null && !searchCondition.getWoFlag().equals("")) {
   criteria.createAlias("woFlag", "i").add(Restrictions.like("i.value", "%" + searchCondition.getWoFlag() + "%").ignoreCase());
  }
  if (searchCondition.getItemCode() != null && !searchCondition.getItemCode().equals("")) {
   criteria.createAlias("item", "j").add(Restrictions.like("j.itemCode", "%" + searchCondition.getItemCode() + "%").ignoreCase());
  }
  if (searchCondition.getItemName() != null && !searchCondition.getItemName().equals("")) {
   criteria.createAlias("item", "k").add(Restrictions.like("k.itemName", "%" + searchCondition.getItemName() + "%").ignoreCase());
  }
  if (searchCondition.getEngineerName() != null && !searchCondition.getEngineerName().equals("")) {
   criteria.createAlias("engineer", "l").add(Restrictions.like("l.engineerName", "%" + searchCondition.getEngineerName() + "%").ignoreCase());
  }
  if (searchCondition.getEvidence() != null && !searchCondition.getEvidence().equals("")) {
   criteria.add(Restrictions.like("cause", "%" + searchCondition.getEvidence() + "%").ignoreCase());
  }
  if (searchCondition.getApplyDept() != null && !searchCondition.getApplyDept().equals("")) {
   criteria.createAlias("dept", "m").add(Restrictions.like("m.departmentName", "%" + searchCondition.getApplyDept() + "%").ignoreCase());
  }
  //此处主要是判断是否全区查询
  if(searchCondition.getIsSelectedAllArea() != null && searchCondition.getIsSelectedAllArea().equals("on")){
//   criteria.createAlias("area", "o").add(Restrictions.like("o.id","%"+searchCondition.getAreaId().toString().substring(0, 4)+"%"));
   criteria.createAlias("area", "o").add(Restrictions.sqlRestriction("AREA_ID  like  (?)","%"+searchCondition.getAreaId().toString().substring(0, 4)+"%",Hibernate.STRING));
  }else{
      if (searchCondition.getAreaId() != null && !searchCondition.getAreaId().equals("")) {
       criteria.createAlias("area", "o").add(Restrictions.eq("o.id", searchCondition.getAreaId()));
      }
  }
  try {
   ToAccessDate toAccessDate;
   toAccessDate = new ToAccessDate(searchDateBean);
   // 进入通过时间进行查询的处理
   criteria = toAccessDate.accessDate(criteria, searchDateBean);
   // 进入总金额的处理
   criteria = toAccessDate.accessSum(criteria, searchDateBean);
  } catch (Exception e) {
   e.printStackTrace();
  }
  criteria.add(Restrictions.eq("deleteFlage", GlobalConstant.DEL_FLAG_FALSE));
  if (searchCondition.getMmCode() != null && !searchCondition.getMmCode().equals("") || searchCondition.getMmName() != null && !searchCondition.getMmName().equals("")) {
   criteria.createCriteria("tradeMMItemList").createAlias("material", "m").add(Restrictions.like("m.materialCode", "%" + searchCondition.getMmCode() + "%").ignoreCase()).add(Restrictions.like("m.materialName", "%" + searchCondition.getMmName() + "%").ignoreCase());
  }
  criteria.addOrder(Order.asc("applyCode"));
  return abstractMMItemDao.findByCriteria(criteria);
 }

阅读更多
个人分类: hibernate相关
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭