午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

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

使用Spring Data Jpa的CriteriaQuery一個陷阱

瀏覽:11日期: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
相關文章:
主站蜘蛛池模板: 亚洲裸体视频 | 成年人视频在线观看免费 | 天天爽夜夜爽人人爽 | 欧美色人阁 | 国产资源av| 国内成人自拍视频 | 久久伦理片 | 香蕉精品视频在线观看 | 久久国产高清 | 黄视频在线播放 | 免费亚洲视频 | 国产成年人在线观看 | 夫妻性生活毛片 | 天天狠狠操 | 一级免费看片 | 欧美久久久久久久久 | 亚洲色图在线视频 | 天堂欧美城网站 | 亚洲美女啪啪 | 国产成人精品免高潮在线观看 | 国产人成一区二区三区影院 | 午夜看片| 在线观看视频中文字幕 | 久操av在线 | 久久精品视频播放 | 天天色官网 | a久久久久 | 久久免费视频1 | 亚洲久久久 | 日韩一区二区在线播放 | 全部免费毛片在线播放高潮 | 狠狠狠狠狠狠 | 国产小视频在线 | 亚洲第三十七页 | 成人午夜影院 | 亚洲成人国产精品 | 国内精品免费视频 | 国产免费播放 | 亚洲自拍小说 | 国产无限资源 | 黄色av播放 |