开发者

Hibernate Criteria Query - nested condition

开发者 https://www.devze.com 2023-01-25 08:02 出处:网络
I can\'t figure out how to create a query like this with Hibernate Criteria synthax select * from x where x.a = \'abc\'and (x.b = \'def\' or x.b = \'ghi\')

I can't figure out how to create a query like this with Hibernate Criteria synthax

select * from x where x.a = 'abc'  and (x.b = 'def' or x.b = 'ghi')

Do you have an idea of how to do that?

I'm Using Hibernate Restriction s开发者_如何学Ctatic methods but I don't understand how to specify the nested 'or' condition


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')).

0

精彩评论

暂无评论...
验证码 换一张
取 消