querydsl中使用predicate報錯:java.lang.IllegalArgumentException: Predicate must not be null!
阿新 • • 發佈:2019-01-09
如題,在使用querydsl結合mongo-jpa查詢時,出現問題。問題是這樣的,controller層程式碼如下:
@GetMapping("/pagebypredicate") public Page<UserVo> pageByPredicate(@QuerydslPredicate(root=User.class)Predicate predicate, Pageable pageable){ Page<User> pages = userService.findByPager(pageable, predicate); return new PageImpl<>(userMapper.entity2Vos(pages), pageable,pages.getTotalElements()); }
當我們呼叫這個介面,預設不傳入任何引數,不會報錯,查詢所有的使用者。
單獨只傳入predicate的引數username=admin也不會報錯。
或者既傳入username=admin,也傳入page=0,size=10也不會報錯。
當單獨傳入page=0,size=10的時候,就報錯,如題所示的錯誤。Predicate must not be null
這就很奇怪了,預設情況下,相當於predicate也是null(事實上是一個empty的BooleanBuilder),但是就不報錯。
其實,預設不傳輸任何引數的情況下,predicate不是為空的,其實是一個empty的BooleanBuilder物件。當我們只傳輸page=0,size=10時,predicate卻成了一個null,並不是empty 的BooleanBuilder物件。這裡使用一個最low的解決辦法。新增一個為空的判斷。如果為空,則構建一個BooleanBuilder物件。
不知道有沒有別的解決辦法。