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?