Hibernate Criteria Query – 嵌套条件

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

select * from x where xa = 'abc' and (xb = 'def' or xb = 'ghi') 

你知道怎么做吗?
我正在使用Hibernate Restriction静态方法,但我不明白如何指定嵌套的’或’条件

您的具体查询可能是:

 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 xa = 'abc' and (xb = 'a' or xb = 'b' or xb = 'c') 

使用更像(xb IN ('def', 'ghi'))的语法。