Spring data JPA and parameters that can be null

后端 未结 6 1899
不知归路
不知归路 2020-12-03 09:45

My understanding is, that with Spring data JPA I cannot have a query method to fetch all rows where a column equals a given non-null method parameter and use the same method

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 10:13

    In my case membershipNumber is nullable, and I have handled it this way. This will handle all the cases where table.membershipNumber is null too.

          @Query(value = "SELECT pr FROM ABCTable pr " +
                "WHERE LOWER(pr.xyz) = LOWER(:xyz) " +
                "and LOWER(pr.subscriptionReference) = LOWER(:subscriptionReference) " +
                "and pr.billId = :billId " +
                "and ((pr.membershipNumber = :membershipId) or (pr.membershipNumber = null and :membershipId = null))")
        List getSomething (@Param("xyz") String xyz,
                                                     @Param("subscriptionReference") String subscriptionReference,
                                                     @Param("billId") Integer billId,
                                                     @Param("membershipId") String membershipNumber);
    
    

提交回复
热议问题