一尘不染

Hibernate Criteria Query-嵌套条件

hibernate

我不知道如何使用Hibernate Criteria synthax创建这样的查询

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

您是否知道该怎么做?
我正在使用Hibernate Restriction静态方法,但不了解如何指定嵌套的“或”条件


阅读 360

收藏
2020-06-20

共1个答案

一尘不染

您的特定查询可能是:

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

如果您想了解一般的AND和OR,请执行以下操作:

// 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')
2020-06-20