使用Spring Data Jpa的CriteriaQuery一个陷阱

 更新时间:2020年11月12日 08:35:38   作者:钟潘  
使用Spring Data Jpa的CriteriaQuery进行动态条件查询时,可能会遇到一个陷阱,当条件为空时,查询不到任何结果,并不是期望的返回所有结果。这是为什么呢?

使用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/2020/07/20/035-a-trap-for-using-criteriaquery/

以上就是CriteriaQuery使用的一个陷阱的详细内容,更多关于CriteriaQuery 陷阱的资料请关注脚本之家其它相关文章!

相关文章

  • mybatis中 if-test 数字判断的坑及解决

    mybatis中 if-test 数字判断的坑及解决

    这篇文章主要介绍了mybatis中 if-test 数字判断的坑及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • java 实现双向链表实例详解

    java 实现双向链表实例详解

    这篇文章主要介绍了java 实现双向链表实例详解的相关资料,需要的朋友可以参考下
    2017-03-03
  • Java中BigDecimal使用注意避坑指南

    Java中BigDecimal使用注意避坑指南

    Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算,下面这篇文章主要给大家介绍了关于Java中BigDecimal使用注意避坑的相关资料,需要的朋友可以参考下
    2023-02-02
  • Java数据结构之图的两种搜索算法详解

    Java数据结构之图的两种搜索算法详解

    在很多情况下,我们需要遍历图,得到图的一些性质。有关图的搜索,最经典的算法有深度优先搜索和广度优先搜索,接下来我们分别讲解这两种搜索算法,需要的可以参考一下
    2022-11-11
  • 关于Integer.parseInt()方法的使用

    关于Integer.parseInt()方法的使用

    这篇文章主要介绍了关于Integer.parseInt()方法的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • SpringSecurityOAuth2实现微信授权登录

    SpringSecurityOAuth2实现微信授权登录

    微信的登录功能是用户注册和使用微信的必经之路之一,而微信授权登录更是方便了用户的登录操作,本文主要介绍了SpringSecurityOAuth2实现微信授权登录,感兴趣的可以了解一下
    2023-09-09
  • Java实现支付宝之第三方支付宝即时到账支付功能

    Java实现支付宝之第三方支付宝即时到账支付功能

    这篇文章主要介绍了Java实现支付宝之第三方支付宝即时到账支付功能的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-07-07
  • 聊聊MultipartFile与File的一些事儿

    聊聊MultipartFile与File的一些事儿

    这篇文章主要介绍了MultipartFile与File的一些事儿,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • spring mvc中@PathVariable / 带斜杠方式获取

    spring mvc中@PathVariable / 带斜杠方式获取

    这篇文章主要介绍了spring mvc中@PathVariable / 带斜杠方式获取,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • 详解Android系统中的root权限获得原理

    详解Android系统中的root权限获得原理

    这篇文章主要介绍了详解Android系统中的Root权限获得原理,安卓基于Linux,所以原理也相当于Linux中的root用户,需要的朋友可以参考下
    2015-08-08

最新评论