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)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.