国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

使用Spring Data Jpa的CriteriaQuery一個陷阱

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

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

例如下述代碼,當predicates為空時,返回結(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的注釋就明白了,因為空條件總是為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);}

所以正確的寫法應(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() ? 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方法中調(diào)用where,toPredicate只需要返回條件,外層會調(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<?> query, CriteriaBuilder criteriaBuilder);}

本文作者: 鐘潘本文鏈接: http://zhongpan.tech/2020/07/20/035-a-trap-for-using-criteriaquery/

以上就是CriteriaQuery使用的一個陷阱的詳細內(nèi)容,更多關(guān)于CriteriaQuery 陷阱的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 亚洲 欧美 日韩中文字幕一区二区 | 日本一级做人免费视频 | 特级一级全黄毛片免费 | 女女同性一区二区三区四区 | 久热精品6 | 免费视频成人 | 成年人在线观看免费 | 高清亚洲 | 在线视频一二三区 | 中文字幕日韩欧美一区二区三区 | 亚洲欧美日韩精品高清 | 成人免费黄网站 | 国产成人黄网址在线视频 | 222aaa天堂 | 欧美亚洲欧美区 | 午夜久久网 | 日本私人色多多 | 成年人在线观看视频免费 | 久久综合日韩亚洲精品色 | 亚洲另类视频在线观看 | 看一级特黄a大片日本片 | 2019偷偷狠狠的日日 | 久久精品一级 | 6080伦理久久亚洲精品 | 99久久国产| 老司机精品福利视频 | 国产日韩在线视频 | 欧美在线一级精品 | 伊人色综合久久天天网蜜月 | 国产一区二区精品在线观看 | 亚洲六月丁香六月婷婷蜜芽 | 欧美日韩另类在线观看视频 | 精品三级内地国产在线观看 | 在线观看亚洲欧美 | 欧美视频精品在线 | 日韩黄在线观看免费视频 | 欧美一级毛片美99毛片 | 成人免费视频在 | 欧美在线播放视频 | 亚洲国产欧美在线成人aaaa | 欧美日本一区二区三区生 |