java 休眠限制和/或命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1048244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hibernate restrictions and/or order
提问by Fortega
small questions about Restrictions.or and Restrictions.and
关于 Restrictions.or 和 Restrictions.and 的小问题
If I do something like this:
如果我做这样的事情:
...
criterion = criterionA;
criterion = Restrictions.and(criterion, criterionB);
criterion = Restrictions.or(criterion, criterionC);
criterion = Restrictions.and(criterion, criterionD);
Will this be treated as:
这是否会被视为:
(A and B) or (C and D) (following mathematical conventions)
Or will it be treated in the order it the restrictions have been added:
还是会按照添加限制的顺序对其进行处理:
(((A and B) or C) and D)
Please also add references if there are any...
如果有的话,还请补充参考...
回答by jitter
It should be treated as the latter
应该作为后者处理
(((A and B) or C) and D)
You could do
你可以做
criterion = Restriction.or(Restrictions.and(criterionA, criterionB), Restrictions.and(criterionC, criterionD))
If you want the first solution
如果你想要第一个解决方案
回答by dfa
there are no precedence rules (like in a programming language or in a CFG parser), method calls order unambiguously determines the expression.
没有优先规则(如在编程语言或 CFG 解析器中),方法调用顺序明确地决定了表达式。
(A and B) or (C and D) must be translated to:
(A 和 B) 或 (C 和 D) 必须翻译成:
import static org.hibernate.criterion.Restrictions.*;
...
criterion = or(and(A, B), and(C,D));

