Hibernate Criteria Query - nested condition

隐身守侯 提交于 2019-12-04 04:12:00

You specific query could be:

crit.add(Restrictions.eq("a", "abc"));
crit.add(Restrictions.in("b", new String[] { "def", "ghi" });

If you're wondering about ANDs and ORs in general, do this:

// Use disjunction() or conjunction() if you need more than 2 expressions
Disjunction aOrBOrC = Restrictions.disjunction(); // A or B or C
aOrBOrC.add(Restrictions.eq("b", "a"));
aOrBOrC.add(Restrictions.eq("b", "b"));
aOrBOrC.add(Restrictions.eq("b", "c"));

// Use Restrictions.and() / or() if you only have 2 expressions
crit.add(Restrictions.and(Restrictions.eq("a", "abc"), aOrBOrC));

This will be equivalent to:

where x.a = 'abc' and (x.b = 'a' or x.b = 'b' or x.b = 'c')

Use a syntax more like (x.b IN ('def', 'ghi')).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!