色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

使用Spring Data Jpa的CriteriaQuery一個陷阱

瀏覽:3日期:2023-08-02 17:10:41

使用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 陷阱的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 色综合美国色农夫网 | 国产成人免费全部网站 | 久久狠狠躁免费观看2020 | 天堂一区二区在线观看 | 欧美三级在线观看不卡视频 | 久久有这有精品在线观看 | 日韩免费精品一级毛片 | 午夜毛片免费观看视频 | 欧美人拘一级毛片 | 成人亲子乱子伦视频 | 免费欧洲毛片a级视频无风险 | 中文国产成人精品久久久 | 欧美成人精品高清在线播放 | 国产成人久久精品区一区二区 | 一级毛片美国一级j毛片不卡 | 日本一区二区三区不卡在线视频 | 色综合久久久 | free性丰满白嫩白嫩的hd | 日本特黄aaaaaaa大片 | 欧美一级毛片在线一看 | 欧美aaa大片| 黄色在线网站 | 毛片网站在线播放 | 欧美黄网站免费观看 | 一级日韩| 欧美一级免费片 | 理伦毛片| 99国产福利视频在线观看 | 免费的特黄特色大片在线观看 | 色偷偷88欧美精品久久久 | 精品日韩欧美一区二区三区在线播放 | 欧美aaa| 国产短视频精品一区二区三区 | 久久久夜间小视频 | 欧美亚洲日本国产综合网 | 欧美理论片在线观看一区二区 | 亚洲m男在线中文字幕 | 米奇777第四久久久99 | 欧美成人午夜不卡在线视频 | 黄色网址视频在线观看 | 亚洲精品久久九九热 |