How do you “OR” criteria together when using a criteria query with hibernate?
我正在尝试使用休眠条件查询在三个字段上做一个基本的"或"。
例
1 2 3 4 5
| class Whatever{
string name;
string address;
string phoneNumber;
} |
我想建立一个条件查询,其中我的搜索字符串可以匹配"名称"或"地址"或" phoneNumber"。
您要使用Restrictions.disjuntion()。 像这样
1 2 3 4 5 6
| session.createCriteria(Whatever.class)
.add(Restrictions.disjunction()
.add(Restrictions.eq("name", queryString))
.add(Restrictions.eq("address", queryString))
.add(Restrictions.eq("phoneNumber", queryString))
); |
请参阅此处的Hibernate文档。
假设您要进行休眠会话,则应执行以下操作:
1 2 3 4 5 6
| 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); |
1 2 3 4 5 6 7 8 9 10
| //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); |
//(c1 OR c2)AND c3或任何复杂的表达式都可以做同样的事情。
1 2 3 4 5 6 7 8 9 10 11 12
| //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. |
万一有人为NHibernate遇到同样的问题,以防万一,
1 2 3 4 5
| ICriteria c = session.CreateCriteria(typeof (Whatever))
.Add(Expression.Disjunction()
.Add(Expression.Eq("name", searchString))
.Add(Expression.Eq("address", searchString))
.Add(Expression.Eq("phoneNumber", searchString))); |
这对我来说,对于OR条件是起作用的,对于IN条件也是如此,而不是在此讨论中投票最多的答案:
1 2 3 4
| 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"})
)); |
结果查询:
1 2 3 4 5 6
| and (
this_.XXXX=?
or this_.YYYY in (
?, ?, ?
)
) |
可以使用或/和在不同级别的查询中应用条件
使用析取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 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(); |
|