Hibernate条件查询 - 查询条件 [英] Hibernate criteria queries - Query Conditions

查看:225
本文介绍了Hibernate条件查询 - 查询条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是spring 3.0,我使用的是JqGrid插件。我正在处理搜索功能,它发送一个带有所有搜索条件的json字符串。

  {groupOp:AND,rules:[{field : 名字, 运算: 体重, 数据: 约翰},{ 场: 姓氏, 运算: CN, 数据: Doe的},{ field:gender,op:eq,data:Male}]} 

如果您查看规则数组内的op属性,您将看到必须执行的操作。 Jq-grid有以下操作:
$ b $ ['eq','ne','lt','le','gt','ge','bw '','bn','in','ni','ew','en','cn','nc']

p>

['equal','not equal','less','less or equal','greater','greater or equal','beginning with','does不以'开头,'''在','不在','以'结束','不以'结束','包含','不包含']



我打算使用休眠标准搜索来启​​用搜索功能。为此,我使用Jackson的 ObjectMapper 将传入的JSON转换为Java。这一切都很好。这是我的代码转换JSON。

  public class JsonJqgridSearchModel {

public String groupOp;

public ArrayList< JqgridSearchCriteria>规则;
}


公共类JqgridSearchCriteria {

公共字符串字段;

public String op;

公共字符串数据;

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)){
返回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)布尔搜索,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< JqgridSearchCriteria> 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 {//在这里做其他事情}

这是问题出现的地方。如何将jqGrid操作转换为等效的hibernate命令?例如
cn应该与

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

解决方案

有趣的是,到你上面的东西(但我不使用JqGrid)。我想知道您的问题是否与cn - LIKE条件特别相关?我遇到了问题 - 我不得不指定MatchMode来像我想要的那样获取contains:

  return Restrictions.ilike (
searchCriterion.getPropertyName(),
searchCriterion.getValue()。toString(),
MatchMode.ANYWHERE);

我发现如果没有指定MatchMode,它会生成SQL为:

  WHERE属性LIKE'值'

通过指定MatchMode.ANYWHERE,它生成SQL为:

  WHERE属性LIKE'%value%'

这是我期待的contains操作。也许这也是你的问题?


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<JqgridSearchCriteria> 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<JqgridSearchCriteria> 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?

这篇关于Hibernate条件查询 - 查询条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆