JPA Criteria API Specification<> 中 toPredicate() 方法与 And/Or/Not/Where 的区别
Difference Between toPredicate() Method and And/Or/Not/Where in JPA Criteria API Specification<>
我现在正在研究用于创建动态查询和类型安全查询的 JPA 标准 API。我正在探索如何使用规范。当我探索时,我发现 toPredicate()
方法用于为实体引用的查询创建 where 子句。还探讨了 And/Or/Not 和规范中的位置。
实际上如果toPredicate()
m方法创建where子句,那么为什么我们使用方法"Where"?比较 toPredicate()
方法时其他方法的作用是什么?
如果您从头开始实施 Specification
,您将实施 toPredicate
。
如果您已经有一个或多个 Specification
,您可以使用 and
、or
和 not
将它们组合成一个新规范。
例如,给定两个规格 isVip
和 placedAnOrderLastMonth
您可以将它们组合成这样:
shouldReceiveNagMail = isVip.and(not(placedAnOrderLastMonth));
where
方法只是人们可能喜欢用来使代码更具可读性的语法糖。只是returns本质上是一样的Specification
;
我现在正在研究用于创建动态查询和类型安全查询的 JPA 标准 API。我正在探索如何使用规范。当我探索时,我发现 toPredicate()
方法用于为实体引用的查询创建 where 子句。还探讨了 And/Or/Not 和规范中的位置。
实际上如果toPredicate()
m方法创建where子句,那么为什么我们使用方法"Where"?比较 toPredicate()
方法时其他方法的作用是什么?
如果您从头开始实施 Specification
,您将实施 toPredicate
。
如果您已经有一个或多个 Specification
,您可以使用 and
、or
和 not
将它们组合成一个新规范。
例如,给定两个规格 isVip
和 placedAnOrderLastMonth
您可以将它们组合成这样:
shouldReceiveNagMail = isVip.and(not(placedAnOrderLastMonth));
where
方法只是人们可能喜欢用来使代码更具可读性的语法糖。只是returns本质上是一样的Specification
;