hibernate的查询条件lt_Hibernate条件查询 - 查询条件

I am using spring 3.0 and I am using the JqGrid plugin. I am working on the search feature which sends a json string with all the search criteria. Here is what the string can look like.

{"groupOp":"AND","rules":[{"field":"firstName","op":"bw","data":"John"},{"field":"lastName","op":"cn","data":"Doe"},{"field":"gender","op":"eq","data":"Male"}]}

If you look at the "op" property inside the rules array, you will see the operation which must be executed. The Jq-grid has the following operations

['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc']

which corresponds with

['equal','not equal', 'less', 'less or equal','greater','greater or equal', 'begins with','does not begin with','is in','is not in','ends with','does not end with','contains','does not contain']

I plan to use hibernate criteria searching to enable the search feature. For this I am using Jackson's ObjectMapper to convert the incoming JSON into Java. This is all well and good. Here is my code that converts the json.

public class JsonJqgridSearchModel {

public String groupOp;

public ArrayList rules;

}

public class JqgridSearchCriteria {

public String field;

public String op;

public String data;

public SimpleExpression getRestriction(){

if(op.equals("cn")){

return Restrictions.like(field, data);

}else if(op.equals("eq")){

return Restrictions.eq(field, data);

}else if(op.equals("ne")){

return Restrictions.ne(field, data);

}else if(op.equals("lt")){

return Restrictions.lt(field, data);

}else if(op.equals("le")){

return Restrictions.le(field, data);

}else if(op.equals("gt")){

return Restrictions.gt(field, data);

}else if(op.equals("ge")){

return Restrictions.ge(field, data);

}else{

return null;

}

}

}

@RequestMapping(value = "studentjsondata", method = RequestMethod.GET)

public @ResponseBody String studentjsondata(@RequestParam("_search") Boolean search ,HttpServletRequest httpServletRequest) {

StringBuilder sb = new StringBuilder();

Format formatter = new SimpleDateFormat("MMMM dd, yyyy");

if(search){

ObjectMapper mapper = new ObjectMapper();

try {

JsonJqgridSearchModel searchModel= mapper.readValue(httpServletRequest.getParameter("filters"), JsonJqgridSearchModel.class);

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

Session session = sessionFactory.openSession();

session.beginTransaction();

Criteria criteria = session.createCriteria(Person.class);

Iterator iterator = searchModel.rules.iterator();

while(iterator.hasNext()){

System.out.println("before");

criteria.add(iterator.next().getRestriction());

System.out.println("after");

}

} catch (JsonParseException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}else{//do other stuff here}

This is where the problem comes in. How do I transalate the jqGrid operation into the equivelent hibernate command ? For example

"cn" should correspond with

criteria.add(Restrictions.like("firstName", myJsonJqgridSearchModel.data));

解决方案

Interestingly, I've just written almost identical code to what you have above (mine doesn't use JqGrid however). I'm wondering if your problem is specifically related to the "cn" - LIKE condition? I had problems with this - I had to specify the MatchMode to get the "contains" like I wanted:

return Restrictions.ilike(

searchCriterion.getPropertyName(),

searchCriterion.getValue().toString(),

MatchMode.ANYWHERE);

I found that without specifying the MatchMode, it was generating SQL as:

WHERE property LIKE 'value'

By specifying the MatchMode.ANYWHERE, it generated SQL as:

WHERE property LIKE '%value%'

which is the "contains" operation that I was expecting. Perhaps this is your issue as well?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值