使用querydsl搜索null

问题描述 投票:2回答:2

我有一个像这样搜索的控制器方法

@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

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

java spring spring-data spring-data-jpa querydsl
2个回答
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
投票

你可以通过myParam.isNull()来做到这一点

© www.soinside.com 2019 - 2024. All rights reserved.