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

您的位置:首頁(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)目的代碼中常見這種寫法:

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)文章:
主站蜘蛛池模板: 91精品一区国产高清在线 | 996re免费热在线视频手机 | 一级毛片视屏 | 亚洲理论在线观看 | 精品视频 九九九 | 老司机成人免费精品视频 | 亚洲精品国产一区二区 | 精品亚洲成a人在线播放 | 91精品久久久久含羞草 | 国产精品分类视频分类一区 | 亚洲一级片免费看 | 草草影院ccyycom浮力影院 | 欧美一区在线观看视频 | 欧美国产永久免费看片 | 精品免费久久久久久久 | 大伊香蕉精品视频在线天堂 | 日本一级在线播放线观看免 | 黄色毛片播放 | 亚洲精品中文字幕一区在线 | 国产高清一区二区三区免费视频 | 日韩视频国产 | 韩国一级淫片视频免费播放 | 曰本女同互慰高清在线观看 | 国产二区自拍 | 另类欧美视频 | 欧美日韩顶级毛片www免费看 | a级毛片免费播放 | 亚洲乱码国产一区网址 | 国产精品成人免费观看 | 中文字幕亚洲一区二区三区 | 国产成人精品福利网站人 | 免费a网址 | 国产区一区二区三区 | 91久久精品国产91久久性色也 | 97精品国产高清在线看入口 | 国产免费高清在线精品一区 | 国产特黄一级毛片特黄 | 一级片大全 | 末成年娇小性色xxxxx | a级淫片| 一级欧美在线的视频 |