溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用Spring Data Jpa中的CriteriaQuery會(huì)遇到什么問(wèn)題

發(fā)布時(shí)間:2020-11-16 16:04:46 來(lái)源:億速云 閱讀:285 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

今天就跟大家聊聊有關(guān)使用Spring Data Jpa中的CriteriaQuery會(huì)遇到什么問(wèn)題,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

使用Spring Data Jpa的CriteriaQuery進(jìn)行動(dòng)態(tài)條件查詢時(shí),可能會(huì)遇到一個(gè)陷阱,當(dāng)條件為空時(shí),查詢不到任何結(jié)果,并不是期望的返回所有結(jié)果。這是為什么呢?

例如下述代碼,當(dāng)predicates為空時(shí),返回結(jié)果總是為空。

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的注釋就明白了,因?yàn)榭諚l件總是為false,而and的空條件總是為true。所以,如果最后是and就沒(méi)有問(wèn)題,只有or的時(shí)候有問(wèn)題。

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);
}

所以正確的寫(xiě)法應(yīng)該這樣:

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;
 }

如果條件為空則返回一個(gè)空conjunction,也就是空的and,總是為true。

公司項(xiàng)目的代碼中常見(jiàn)這種寫(xiě)法:

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;
}

也能正常工作,但是其實(shí)沒(méi)有必要在toPredicate方法中調(diào)用where,toPredicate只需要返回條件,外層會(huì)調(diào)用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);
}

看完上述內(nèi)容,你們對(duì)使用Spring Data Jpa中的CriteriaQuery會(huì)遇到什么問(wèn)題有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI