最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Spring Data Jpa的CriteriaQuery一個陷阱

 更新時間:2020年11月12日 08:35:38   作者:鐘潘  
使用Spring Data Jpa的CriteriaQuery進行動態(tài)條件查詢時,可能會遇到一個陷阱,當條件為空時,查詢不到任何結(jié)果,并不是期望的返回所有結(jié)果。這是為什么呢?

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

所以正確的寫法應該這樣:

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)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • springboot 使用QQ郵箱發(fā)送郵件的操作方法

    springboot 使用QQ郵箱發(fā)送郵件的操作方法

    這篇文章主要介紹了springboot使用QQ郵箱發(fā)送郵件功能,本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-10-10
  • Java實現(xiàn)的打印螺旋矩陣算法示例

    Java實現(xiàn)的打印螺旋矩陣算法示例

    這篇文章主要介紹了Java實現(xiàn)的打印螺旋矩陣算法,結(jié)合完整實例形式詳細分析了java打印螺旋矩陣的算法原理與實現(xiàn)技巧,需要的朋友可以參考下
    2019-10-10
  • maven插件spring-boot-starter-tomcat的使用方式

    maven插件spring-boot-starter-tomcat的使用方式

    這篇文章主要介紹了maven插件spring-boot-starter-tomcat的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 使用Spring Batch實現(xiàn)批處理任務的詳細教程

    使用Spring Batch實現(xiàn)批處理任務的詳細教程

    在企業(yè)級應用中,批處理任務是不可或缺的一部分,它們通常用于處理大量數(shù)據(jù),如數(shù)據(jù)遷移、數(shù)據(jù)清洗、生成報告等,Spring Batch是Spring框架的一部分,本文將介紹如何使用Spring Batch與SpringBoot結(jié)合,構(gòu)建和管理批處理任務,需要的朋友可以參考下
    2024-06-06
  • Java8新特性:函數(shù)式編程

    Java8新特性:函數(shù)式編程

    Java8最新引入函數(shù)式編程概念,該項技術(shù)可以大大提升編碼效率,本文會對涉及的對象等進行兩種方法的對比,對新技術(shù)更直白的看到變化,更方便學習
    2021-06-06
  • Java報錯:Java.io.FileNotFoundException解決方法

    Java報錯:Java.io.FileNotFoundException解決方法

    這篇文章主要介紹了Java.io.FileNotFoundException的產(chǎn)生原因和解決方法,造成這個報錯的原因可能有文件路徑錯誤、文件被刪除或移動和權(quán)限問題,文中將解決的辦法介紹的非常詳細,需要的朋友可以參考下
    2024-12-12
  • Java鎖之自旋鎖詳解

    Java鎖之自旋鎖詳解

    這篇文章主要介紹了Java鎖之自旋鎖詳解,本文是系列文章的第一篇,請持續(xù)關(guān)注腳本之家java欄目,需要的朋友可以參考下
    2014-09-09
  • 關(guān)于批量插入或更新數(shù)據(jù)(MyBatis-plus框架)

    關(guān)于批量插入或更新數(shù)據(jù)(MyBatis-plus框架)

    這篇文章主要介紹了關(guān)于批量插入或更新數(shù)據(jù)(MyBatis-plus框架),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 如何區(qū)分JAVA中的throws和throw

    如何區(qū)分JAVA中的throws和throw

    這篇文章主要介紹了如何區(qū)分JAVA中的throws和throw,文中講解十分詳細,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-06-06
  • Java發(fā)送郵箱驗證碼、session校驗功能

    Java發(fā)送郵箱驗證碼、session校驗功能

    本篇主要描述“發(fā)送郵箱驗證碼、session校驗”相關(guān)前(html\js)后(java)臺代碼,業(yè)務邏輯示例,需要的朋友可以參考下
    2018-02-02

最新評論

贡山| 陕西省| 航空| 英超| 扬中市| 奉化市| 四会市| 白玉县| 南宫市| 北安市| 大渡口区| 治县。| 肥乡县| 门源| 青河县| 沙湾县| 澳门| 龙南县| 慈利县| 蕉岭县| 抚松县| 蛟河市| 时尚| 江川县| 固始县| 潜江市| 茂名市| 博兴县| 常州市| 洛南县| 德庆县| 平泉县| 从江县| 武汉市| 罗城| 宜宾市| 门头沟区| 甘肃省| 客服| 铜陵市| 黄冈市|