Java 在使用 Hibernate 的条件查询时,您如何将条件“或”在一起?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/57484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:45:05  来源:igfitidea点击:

How do you "OR" criteria together when using a criteria query with hibernate?

javahibernate

提问by ScArcher2

I'm trying to do a basic "OR" on three fields using a hibernate criteria query.

我正在尝试使用休眠条件查询对三个字段执行基本的“或”操作。

Example

例子

class Whatever{
 string name;
 string address;
 string phoneNumber;
}

I'd like to build a criteria query where my search string could match "name" or "address" or "phoneNumber".

我想构建一个条件查询,其中我的搜索字符串可以匹配“姓名”或“地址”或“电话号码”。

采纳答案by sblundy

You want to use Restrictions.disjuntion(). Like so

您想使用Restrictions.disjuntion(). 像这样

session.createCriteria(Whatever.class)
    .add(Restrictions.disjunction()
        .add(Restrictions.eq("name", queryString))
        .add(Restrictions.eq("address", queryString))
        .add(Restrictions.eq("phoneNumber", queryString))
    );

See the Hibernate doc here.

请参阅此处的 Hibernate 文档。

回答by Rob Oxspring

Assuming you have a hibernate session to hand then something like the following should work:

假设您有一个休眠会话要处理,那么以下内容应该可以工作:

Criteria c = session.createCriteria(Whatever.class);
Disjunction or = Restrictions.disjunction();
or.add(Restrictions.eq("name",searchString));
or.add(Restrictions.eq("address",searchString));
or.add(Restrictions.eq("phoneNumber",searchString));
c.add(or);

回答by Geir-Tore Lindsve

Just in case anyone should stumble upon this with the same question for NHibernate:

以防万一有人遇到同样的 NHibernate 问题:

ICriteria c = session.CreateCriteria(typeof (Whatever))
    .Add(Expression.Disjunction()
        .Add(Expression.Eq("name", searchString))
        .Add(Expression.Eq("address", searchString))
        .Add(Expression.Eq("phoneNumber", searchString)));

回答by Dharmender Rawat

    //Expression :  (c1 AND c2) OR (c3)      


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

      Criterion c1 = Restrictions.like("name", "%e%");
      Criterion c2 = Restrictions.ge("salary", 10000.00);
      Criterion c3 = Restrictions.like("name", "%YYY%");
      Criterion c4 = Restrictions.or(Restrictions.and(c1, c2), c3);
      criteria.add(c4);

//Same thing can be done for (c1 OR c2) AND c3, or any complex expression.

//同样的事情可以对 (c1 OR c2) AND c3 或任何复杂的表达式做。

回答by Dharmender Rawat

//Expression :  (c1 AND c2) OR (c3)      


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

  Criterion c1 = Restrictions.like("name", "%e%");
  Criterion c2 = Restrictions.ge("salary", 10000.00);
  Criterion c3 = Restrictions.like("name", "%YYY%");
  Criterion c4 = Restrictions.or(Restrictions.and(c1, c2), c3);
  criteria.add(c4);

  //Same thing can be done for (c1 OR c2) AND c3, or any complex expression.

回答by Tiago Medici

The conditions can be applied using the or / and in different levels of the query using disjunction

可以使用 or / 和在不同级别的查询中使用分离来应用条件

Criteria query = getCriteria("ENTITY_NAME");
query.add(Restrictions.ne("column Name", current _value));

Disjunction disjunction = Restrictions.disjunction();

if (param_1 != null)
    disjunction.add(Restrictions.or(Restrictions.eq("column Name", param1)));

if (param_2 != null)
    disjunction.add(Restrictions.or(Restrictions.eq("column Name", param_2)));

if (param_3 != null)
    disjunction.add(Restrictions.or(Restrictions.eq("column Name", param_3)));
if (param_4 != null && param_5 != null)
    disjunction.add(Restrictions.or(Restrictions.and(Restrictions.eq("column Name", param_4 ), Restrictions.eq("column Name", param_5 ))));

if (disjunction.conditions() != null && disjunction.conditions().iterator().hasNext())
    query.add(Restrictions.and(disjunction));

return query.list();

回答by ronak

This is what worked for me for an OR condition, that too with an IN condition and not the answer up-voted most on this discussion:

这对我来说适用于 OR 条件,也适用于 IN 条件,而不是在本次讨论中投票最多的答案:

criteria.add( Restrictions.or(
                    Restrictions.eq(ch.getPath(ch.propertyResolver().getXXXX()), "OR_STRING"),
                        Restrictions.in(ch.getPath(ch.propertyResolver().getYYYY()), new String[]{"AA","BB","CC"})
                    ));

Resulting Query:

结果查询:

  and (
            this_.XXXX=? 
            or this_.YYYY in (
                ?, ?, ?
            )
        )