spring-data-jpa - JPA Criteria API 规范<>中 toPredicate() 方法和 And/Or/Not/Where 之间的区别
问题描述
我现在正在探索用于创建动态查询和类型安全查询的 JPA Criteria API。我正在探索如何使用规范。当我探索时,我发现该toPredicate()
方法用于为实体引用的查询创建 where 子句。还探索了 And/Or/Not 以及规范中的位置。
实际上,如果toPredicate()
mMethod 创建 where 子句,那么为什么我们使用方法“Where”?比较方法时其他方法的作用是什么toPredicate()
?
解决方案
如果您Specification
从头开始实施 a ,则实施toPredicate
.
如果您已经有一个或多个Specification
s,您可以使用and
、or
和not
将它们组合成一个新规范。
例如,给定两个规范isVip
,placedAnOrderLastMonth
您可以将它们组合为:
shouldReceiveNagMail = isVip.and(not(placedAnOrderLastMonth));
该where
方法只是人们可能喜欢用来使代码更具可读性的语法糖。它只是返回基本相同Specification
;
推荐阅读
- python - 如何合并两个重叠的数据框
- php - 如何在php中获取所选文件的客户端绝对路径
- python - if else 语句中的变量没有得到更新
- ruby-on-rails - 当使用“heroku run rails test”时,我收到错误“将`gem'rails-controller-testing'`添加到您的Gemfile”,它已经添加到我的gemfile
- django - 删除未使用的父模型
- javascript - 超过 Appium + Mocha + WebDriverIO 异步超时
- python - 无效的设备序号,CUDA / TORCH
- python - TensorFlow:损失矩阵函数
- asp.net-mvc - 带有 SignalR 和 Azure Web 服务的 ERR_CONNECTION_RESET
- java - Java 项目的不稳定的 Oracle 数据库连接