首页 > 解决方案 > JPA Criteria API 规范<>中 toPredicate() 方法和 And/Or/Not/Where 之间的区别

问题描述

我现在正在探索用于创建动态查询和类型安全查询的 JPA Criteria API。我正在探索如何使用规范。当我探索时,我发现该toPredicate()方法用于为实体引用的查询创建 where 子句。还探索了 And/Or/Not 以及规范中的位置。

实际上,如果toPredicate()mMethod 创建 where 子句,那么为什么我们使用方法“Where”?比较方法时其他方法的作用是什么toPredicate()

标签: spring-data-jpajpa-criteria

解决方案


如果您Specification从头开始实施 a ,则实施toPredicate.

如果您已经有一个或多个Specifications,您可以使用andornot将它们组合成一个新规范。

例如,给定两个规范isVipplacedAnOrderLastMonth您可以将它们组合为:

shouldReceiveNagMail = isVip.and(not(placedAnOrderLastMonth));

where方法只是人们可能喜欢用来使代码更具可读性的语法糖。它只是返回基本相同Specification


推荐阅读