簡體   English   中英

Hibernate Criteria Query - 嵌套條件

[英]Hibernate Criteria Query - nested condition

我無法弄清楚如何使用Hibernate Criteria synthax創建這樣的查詢

select * from x where x.a = 'abc'  and (x.b = 'def' or x.b = '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 x.a = 'abc' and (x.b = 'a' or x.b = 'b' or x.b = 'c')

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM