使用Spring Data Jpa的CriteriaQuery一個(gè)陷阱
使用Spring Data Jpa的CriteriaQuery進(jìn)行動(dòng)態(tài)條件查詢時(shí),可能會遇到一個(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就沒有問題,只有or的時(shí)候有問題。
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í)沒有必要在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使用的一個(gè)陷阱的詳細(xì)內(nèi)容,更多關(guān)于CriteriaQuery 陷阱的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用Java 壓縮文件打包tar.gz 包的詳細(xì)教程
本文帶領(lǐng)大家學(xué)習(xí)如何使用Java 壓縮文件打包tar.gz 包,主要通過 Apache compress 工具打包,通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2021-05-05Java實(shí)現(xiàn)用位運(yùn)算維護(hù)狀態(tài)碼
位運(yùn)算是一種非常高效的運(yùn)算方式,在算法考察中比較常見,那么業(yè)務(wù)代碼中我們?nèi)绾问褂梦贿\(yùn)算呢,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2024-03-03Java遠(yuǎn)程執(zhí)行shell命令出現(xiàn)java: command not found問題及解決
這篇文章主要介紹了Java遠(yuǎn)程執(zhí)行shell命令出現(xiàn)java: command not found問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。2023-07-07jdbc鏈接遠(yuǎn)程數(shù)據(jù)庫進(jìn)行修改url操作
這篇文章主要為大家詳細(xì)介紹了jdbc鏈接遠(yuǎn)程數(shù)據(jù)庫進(jìn)行修改url操作,感興趣的小伙伴們可以參考一下2016-06-06Hibernate三種狀態(tài)和Session常用的方法
本文主要介紹了Hibernate三種狀態(tài)和Session常用的方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-03-03微信公眾號開發(fā)之設(shè)置自定義菜單實(shí)例代碼【java版】
這篇文章主要介紹了微信公眾號開發(fā)之設(shè)置自定義菜單實(shí)例代碼,本實(shí)例是為了實(shí)現(xiàn)在管理后臺實(shí)現(xiàn)微信菜單的添加刪除管理。需要的朋友可以參考下2018-06-06Java實(shí)現(xiàn)將漢字轉(zhuǎn)化為漢語拼音的方法
這篇文章主要介紹了Java實(shí)現(xiàn)將漢字轉(zhuǎn)化為漢語拼音的方法,實(shí)例演示了Java引用pinyin4j庫實(shí)現(xiàn)漢子轉(zhuǎn)化成拼音的使用技巧,需要的朋友可以參考下2015-12-12