在使用带有hibernate的条件查询时,如何将“OR”标准放在一起?

时间:2022-01-04 19:40:32

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

我正在尝试使用hibernate条件查询在三个字段上执行基本的“OR”。

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".

我想构建一个条件查询,我的搜索字符串可以匹配“name”或“address”或“phoneNumber”。

6 个解决方案

#1


124  

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文档。

#2


62  

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

假设你有一个hibernate会话,那么类似下面的东西应该工作:

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);

#3


9  

    //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或任何复杂表达式执行相同的操作。

#4


6  

//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.

#5


3  

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)));

#6


1  

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();

#1


124  

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文档。

#2


62  

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

假设你有一个hibernate会话,那么类似下面的东西应该工作:

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);

#3


9  

    //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或任何复杂表达式执行相同的操作。

#4


6  

//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.

#5


3  

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)));

#6


1  

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();