使用Spring Data Jpa的CriteriaQuery一个陷阱
#代码知识 发布时间: 2026-01-12
使用Spring Data Jpa的CriteriaQuery进行动态条件查询时,可能会遇到一个陷阱,当条件为空时,查询不到任何结果,并不是期望的返回所有结果。这是为什么呢?

例如下述代码,当predicates为空时,返回结果总是为空。
public Page<VmhostWithRelationPO> listVmhostSpecWithRelationByPage(String name) {
Specification<VmhostWithRelationPO> spec = (root, cq, cb) -> {
root.join("user", JoinType.LEFT);
root.join("tenant", JoinType.LEFT);
List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>();
......
return cb.or(predicates.toArray(new javax.persistence.criteria.Predicate[0]));
};
PageRequest pagable = PageRequest.of(0, 5);
Page<VmhostWithRelationPO> page = vmhostSpecWithRelationDao.findAll(spec, pagable);
return page;
}
看下or的注释就明白了,因为空条件总是为false,而and的空条件总是为true。所以,如果最后是and就没有问题,只有or的时候有问题。
public interface CriteriaBuilder {
/**
* Create a conjunction of the given restriction predicates.
* A conjunction of zero predicates is true.
* @param restrictions zero or more restriction predicates
* @return and predicate
*/
Predicate and(Predicate... restrictions);
/**
* Create a disjunction of the given restriction predicates.
* A disjunction of zero predicates is false.
* @param restrictions zero or more restriction predicates
* @return or predicate
*/
Predicate or(Predicate... restrictions);
}
所以正确的写法应该这样:
public Page<VmhostWithRelationPO> listVmhostSpecWithRelationByPage(String name) {
Specification<VmhostWithRelationPO> spec = (root, cq, cb) -> {
root.join("user", JoinType.LEFT);
root.join("tenant", JoinType.LEFT);
List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>();
......
return predicates.isEmpty() ? cb.conjunction() : cb.or(predicates.toArray(new javax.persistence.criteria.Predicate[0]));
};
PageRequest pagable = PageRequest.of(0, 5);
Page<VmhostWithRelationPO> page = vmhostSpecWithRelationDao.findAll(spec, pagable);
return page;
}
如果条件为空则返回一个空conjunction,也就是空的and,总是为true。
公司项目的代码中常见这种写法:
public Page<VmhostWithRelationPO> listVmhostSpecWithRelationByPage(String name) {
Specification<VmhostWithRelationPO> spec = (root, cq, cb) -> {
root.join("user", JoinType.LEFT);
root.join("tenant", JoinType.LEFT);
List<javax.persistence.criteria.Predicate> predicates = new ArrayList<>();
......
if (predicates.isEmpty()) {
cq.where();
} else {
cq.where(cb.or(predicates.toArray(new javax.persistence.criteria.Predicate[0])));
}
return cq.getRestriction();
};
PageRequest pagable = PageRequest.of(0, 5);
Page<VmhostWithRelationPO> page = vmhostSpecWithRelationDao.findAll(spec, pagable);
return page;
}
也能正常工作,但是其实没有必要在toPredicate方法中调用where,toPredicate只需要返回条件,外层会调用where。
public interface Specification<T> extends Serializable {
/**
* Creates a WHERE clause for a query of the referenced entity in form of a {@link Predicate} for the given
* {@link Root} and {@link CriteriaQuery}.
*
* @param root must not be {@literal null}.
* @param query must not be {@literal null}.
* @param criteriaBuilder must not be {@literal null}.
* @return a {@link Predicate}, may be {@literal null}.
*/
@Nullable
Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder);
}
本文作者: 钟潘
本文链接: http://zhongpan.tech/2025/07/20/035-a-trap-for-using-criteriaquery/
以上就是CriteriaQuery使用的一个陷阱的详细内容,更多关于CriteriaQuery 陷阱的资料请关注其它相关文章!
代码知识SEO上一篇 : 使用Vant完成DatetimePicker 日期的选择器操作
下一篇 : js前端传json后台接收‘‘被转为quot的问题解决
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!