2016-04-19 39 views
2

我有搜索像這樣由空搜索與querydsl

@RequestMapping(method = RequestMethod.GET) 
public Page<MyEntity> find(@QuerydslPredicate(root = MyEntity.class) 
            Predicate predicate, 
         Pageable pageable) { 

    return this.myEntityRepository.findAll(predicate, pageable); 
} 

它的偉大工程的控制方法。我可以使用各種查詢字符串參數發出GET請求,並且相應地進行過濾,但現在我想通過null進行搜索。我試着做類似/myentity?myParam1=&,但predicate的論點總是null

如何搜索特定字段爲空的位置?

回答

1

無法通過像myParam1=null這樣的空參數搜索實體,因爲它會引發NullPointerException。 我有同樣的問題,這是我能使它工作的唯一方法。


@RequestMapping(method = RequestMethod.GET) 
public Page find(@QuerydslPredicate(root = MyEntity.class) Predicate predicate, Pageable pageable, @RequestParam MultiValueMap parameters) { 

    Page entities = myEntityRepository.findAll(predicate, pageable); 
    if(parameters.get("myParam1") != null && 
      parameters.get("myParam1").get(0) != null && 
      parameters.get("myParam1").get(0).isEmpty()) { 

     QEntity entity = QEntity.entity; 
     BooleanExpression myParam1IsNull = entity.in(entities.getContent()).and(entities.myParam1.isNull()); 
     entities = myEntityRepository.findAll(myParam1IsNull, pageable); 
    } 

    return entities; 
} 

它執行兩個查詢到數據庫,但它解決了問題。 我希望這可以幫助你。

+0

我最終創建了具有硬編碼行爲的其他資源/路由。不過謝謝。 –