Java 使用逻辑 AND 处理休眠多个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1716259/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:50:30  来源:igfitidea点击:

Working with hibernate multiple Criteria with logical AND

javahibernateormjakarta-ee

提问by black sensei

So far i've been working with only a case with 2 properties with and as logical operator so i use LogicalExpression like so

到目前为止,我一直只处理一个具有 2 个属性的案例,并且作为逻辑运算符,所以我像这样使用 LogicalExpression

Criterion eqRef = Restrictions.eq("referenceID", referenceId);
Criterion eqType = Restrictions.eq("verificationType", type);
LogicalExpression and = Restrictions.and(eqRef, eqType);

this time along its more than 2 so i'm a bit confused.say this time i've added username property i can do the normal chaining with

这次超过 2 个,所以我有点困惑。说这次我添加了用户名属性,我可以使用它进行正常的链接

session.createCriteria(this.getPersistentClass())
.add(Restrictions.eq("referenceID", referenceId))
.add(Restrictions.eq("verificationType", type))
.add(Restrictions.eq("username", username))
.list();

but now i don't know which kind of logical operator used between them.i'm also tempted to do this:

但现在我不知道它们之间使用了哪种逻辑运算符。我也很想这样做:

Criterion eqRef = Restrictions.eq("referenceID", referenceId);
Criterion eqType = Restrictions.eq("verificationType", type);
Criterion equsername = Restrictions.eq("username", username);
LogicalExpression and = Restrictions.and(eqRef, eqType);
LogicalExpression secondand = Restrictions.and(and, equsername);

i've see the eqAll too but i've never used it before.So do you have any idea about that?How do you do it, and thanks for reading this.

我也看过 eqAll,但我以前从未使用过它。所以你对此有什么想法吗?你是怎么做到的,感谢阅读本文。

采纳答案by Steve B.

Since you're "AND"-ing all of your restrictions the grouping is irrelevant, so you can just continue on as in your second example, as there's nothing to be gained by subgrouping them, e.g.

由于您对所有限制进行了“与”操作,因此分组无关紧要,因此您可以像第二个示例一样继续操作,因为对它们进行分组没有任何好处,例如

.add(Restrictions.eq("referenceID", referenceId)).add(Restrictions.eq("verificationType", type)).add(Restrictions.eq("username", username))...

You do run into this problem where you need to group mixed "AND" and "OR" queries, if you want to group multiple "OR" values you can also add Criteria to a Disjunction

您确实遇到了这个问题,您需要对混合的“AND”和“OR”查询进行分组,如果您想对多个“OR”值进行分组,您还可以将 Criteria 添加到Disjunction

回答by Yuval

Adding these restrictions individually creates a relation of 'AND' between them. This is good enough for what you need.

单独添加这些限制会在它们之间创建“AND”关系。这足以满足您的需要。

回答by lucas

but now i don't know which kind of logical operator used between them.

但现在我不知道它们之间使用了哪种逻辑运算符。

Good answers already, but as an aside you can use the property hibernate.show_sql, set it to true, and you'll be able to see exactly what sql your criteria is producing.

好的答案已经,但顺便说一句,您可以使用 property hibernate.show_sql,将其设置为 true,并且您将能够准确地看到您的标准正在生成什么 sql。

回答by irbef

just use this

就用这个

criteria = session.createCriteria(Model.class).add(
                             Restrictions.between("ModelDate", dateMin, dateMax));