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

原创 2007年10月12日 18:22:00

 在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的Restrictions对数字型即整型进行模糊查询

// TEARCHER_ID是数据库里面的字段。而id是代码中的参数值。 Restrictions.sqlRestriction("CAST({alias}.TEARCHER_ID AS CHAR) ...
  • qq416647781
  • qq416647781
  • 2015年04月15日 13:48
  • 1323

Hibernate hql对Integer类型的模糊查询

hibernate hql对Integer类型的模糊查询 会报错:第一种有问题的方案:whereHql = whereHql + " and o.puid like :id"; par...
  • FANGAOHUA200
  • FANGAOHUA200
  • 2016年12月18日 23:00
  • 1288

HQL实现模糊查询两种传参方式

其实各个方法的实质都是一样的,只不过传递参数的方法稍微有点区别  第一种:  public List getUsers(String id){ List list=new ArrayList...
  • csdn_wyl
  • csdn_wyl
  • 2015年11月22日 15:23
  • 6578

Spring中常用的hql查询方法 (getHibernateTemplate())

一、find(String queryString);       示例:this.getHibernateTemplate().find("from bean.User");      ...
  • qq_22498277
  • qq_22498277
  • 2016年08月09日 16:36
  • 3201

getHibernateTemplate()的find用法大全

一、find(String queryString);      示例:this.getHibernateTemplate().find("from bean.User");      返回所...
  • archer119
  • archer119
  • 2016年05月25日 17:04
  • 5535

hibernate映射Oracle中LONG类型

  • 2013年01月11日 11:35
  • 163KB
  • 下载

Hibernate HQL like 语句的用法

String str = "中文字符串";String hql = "FROM Table AS t WHERE t.field like '%" + str + "%'";Query query =...
  • forever8tf
  • forever8tf
  • 2011年01月18日 14:25
  • 30187

Hibernate实现模糊查询的方法

大家都知道使用本地SQL进行模糊查询用关键字 LIKE 对于在Hibernate里怎么实现呢? 一、绑定参数法: Sql代码 Session session=...
  • jianhuili
  • jianhuili
  • 2012年05月22日 13:17
  • 12484

LONG的模糊查询的问题

oracle是强烈不建议使用LANG类型的,因为它有太多弊端,很不灵活,bi
  • qinyu100
  • qinyu100
  • 2014年06月22日 20:03
  • 1638

关于Hibernate底层的查询处理(忽略大小写,模糊匹配,自动加载属性)

①模糊匹配 like("属性",条件) ②忽略大小写 li
  • xb12369
  • xb12369
  • 2014年08月28日 17:05
  • 4165
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hibernate模糊查询id为long型的情况
举报原因:
原因补充:

(最多只允许输入30个字)