简体   繁体   English

通过多个可选参数过滤 api

[英]Filter api by multiple optional parameters

I am trying to filter repository method using multiple optional parameters.我正在尝试使用多个可选参数来过滤存储库方法。 But I am not getting expected result.但我没有得到预期的结果。 Here is my query.这是我的查询。

Here visitor entity contains multiple visits and one visits can have one contact person and one timeslot.这里访客实体包含多次访问,一次访问可以有一个联系人和一个时间段。

Thanks for the help谢谢您的帮助

@Query("select v from Visitor v join v.visits visits join visits.contactPerson cp where "
            + "v.firstName=:firstName or :firstName is NULL or :firstName = '' and "
            + "visits.approvalStatus=:approvalStatus or :approvalStatus is NULL or :approvalStatus = '' "
            + "and cp.firstName=:firstName or :firstName is NULL or :firstName = '' ")
    public List<Visitor> findByFilter(@Param("firstName") String firstName,
            @Param("approvalStatus") String approvalStatus, @Param("firstName") String fName);

You need to add parentheses to make the database understand your query the way you intend:您需要添加括号以使数据库按照您的意图理解您的查询:

 + "(v.firstName=:firstName or :firstName is NULL or :firstName = '') and "
 + "(visits.approvalStatus=:approvalStatus or :approvalStatus is NULL or :approvalStatus = '') "
 + "and (cp.firstName=:firstName or :firstName is NULL or :firstName = '')"

The OR operator has a lower precedence than AND so a OR b AND c OR d is parsed as a OR (b AND c) OR d , not as (a OR b) AND (c OR d) . OR 运算符的优先级低于 AND,因此a OR b AND c OR d被解析为a OR (b AND c) OR d ,而不是(a OR b) AND (c OR d)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM