Hibernate Criteria Query - 嵌套条件 [英] Hibernate Criteria Query - nested condition

查看:191
本文介绍了Hibernate Criteria Query - 嵌套条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何使用Hibernate Criteria synthax创建这样的查询

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

你有吗?如何做到这一点的想法?

我正在使用Hibernate Restriction静态方法,但我不明白如何指定嵌套'或'条件

Do you have an idea of how to do that?
I'm Using Hibernate Restriction static methods but I don't understand how to specify the nested 'or' condition

推荐答案

您的具体查询可能是:

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

如果您对AND和OR一般感到疑惑,请执行以下操作:

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

这相当于:

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

这篇关于Hibernate Criteria Query - 嵌套条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆