问题描述:Hibernate下做模糊匹配不区分大小写的时候,控制打印出来的脚本出现两个coornateName,明明我只传了一个,为什么会有两个?
①模糊匹配 like("属性",条件)
②忽略大小写 like("属性",条件).ignoreCase()
原因:HibrenateUtils.createDetachedCriteria(class,true) 会自动加载属性,也就是说只要action接收了属性对应的值,查询的时候,就会自动给该值做匹配(=)
原始脚本:
select this_.id as id24_0_, this_.axisId as axisId24_0_, this_.isLock as isLock24_0_, this_.addDate as addDate24_0_, this_.coordinateName as coordina5_24_0_, this_.PROCESSID as PROCESSID24_0_, this_.parentId as parentId24_0_, this_.coordinateValue as coordina8_24_0_, this_.isValid as isValid24_0_, this_.adduser as adduser24_0_ from t_coordinate this_ where this_.coornaditeName = ? <pre name="code" class="sql"><span style="white-space:pre"> </span>and lower(this_.coordinateName) like ?and this_.axisId=? order by this_.addDate desc
select this_.id as id24_0_, this_.axisId as axisId24_0_, this_.isLock as isLock24_0_, this_.addDate as addDate24_0_, this_.coordinateName as coordina5_24_0_, this_.PROCESSID as PROCESSID24_0_, this_.parentId as parentId24_0_, this_.coordinateValue as coordina8_24_0_, this_.isValid as isValid24_0_, this_.adduser as adduser24_0_ from t_coordinate this_ where this_.axisId=? and lower(this_.coordinateName) like ? order by this_.addDate desc
完整的java代码:
public Page getCoordByPage(Page queryHandler, TCoordinate conditions) { DetachedCriteria dc = DetachedCriteria.forClass(TCoordinate.class); if (conditions.getTAxis() != null) { dc.add(Restrictions.eq("TAxis", conditions.getTAxis())); } if(conditions.getCoordinateName()!=null){ dc.add(Restrictions.like("coordinateName", conditions.getCoordinateName(),MatchMode.ANYWHERE).ignoreCase()); } if(conditions.getParentCoordinate().getId()!=null){ dc.add(Restrictions.eq("parentCoordinate.id", conditions.getParentCoordinate().getId())); } dc.addOrder(Order.desc("addDate")); Page page = this.coordinateDao.getPageByCriteria(queryHandler, dc); return page; }
dc.add(Restrictions.like("coordinateName", conditions.getCoordinateName(),MatchMode.ANYWHERE).ignoreCase());