久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

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

使用Spring Data Jpa的CriteriaQuery一個(gè)陷阱

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

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

所以正確的寫法應(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; }

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

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

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<?> query, CriteriaBuilder criteriaBuilder);}

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

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 欧美自拍视频 | 亚洲精品视频在线 | 日韩午夜免费视频 | 久久99精品久久久水蜜桃 | 欧美日韩中文字幕 | 91在线视频免费观看 | 91麻豆精品国产91久久久久久久久 | 精品无人乱码一区二区三区 | 美女午夜视频 | 久久久www | 亚洲福利av | 亚洲视频在线观看 | 欧美日韩不卡合集视频 | 久久久久一区二区 | japan护士性xxxⅹhd | 亚洲视频免费 | 97国产精品视频 | 亚洲精品一区久久久久久 | 国产色 | 成人精品视频在线观看 | 国产一区二区精品 | 国产精品久久久久久久久久ktv | 能在线观看的黄色网址 | 亚洲女人的天堂 | 91视频大全 | 欧美精品在线一区二区 | 国产乱叫456 | 色婷婷国产精品免费网站 | 国产精品一区一区三区 | 99精品视频一区二区三区 | 日本三级网站在线观看 | 久久久精品456亚洲影院 | 羞羞的视频在线免费观看 | 97碰碰碰免费公开在线视频 | 久久久999精品视频 欧美老妇交乱视频 | 日韩婷婷| 精品成人免费一区二区在线播放 | 欧美久久久久 | 亚洲欧美一区二区三区久久 | 国产精品国产精品国产专区不卡 | 国产高清精品一区二区三区 |