mic*_*son 7 java sql hibernate criteria
我无法弄清楚如何使用Hibernate Criteria synthax创建这样的查询
select * from x where x.a = 'abc' and (x.b = 'def' or x.b = 'ghi')
Run Code Online (Sandbox Code Playgroud)
你知道怎么做吗?
我正在使用Hibernate Restriction静态方法,但我不明白如何指定嵌套的'或'条件
DaG*_*RRz 14
您的具体查询可能是:
crit.add(Restrictions.eq("a", "abc"));
crit.add(Restrictions.in("b", new String[] { "def", "ghi" });
Run Code Online (Sandbox Code Playgroud)
如果您一般想知道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));
Run Code Online (Sandbox Code Playgroud)
这相当于:
where x.a = 'abc' and (x.b = 'a' or x.b = 'b' or x.b = 'c')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5463 次 |
最近记录: |