温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Spring Data Jpa中的CriteriaQuery会遇到什么问题

发布时间:2020-11-16 16:04:46 来源:亿速云 阅读:275 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关使用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() &#63; 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<&#63;> query, CriteriaBuilder criteriaBuilder);
}

看完上述内容,你们对使用Spring Data Jpa中的CriteriaQuery会遇到什么问题有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI