0

I've read a number of posts on here relating to JPA handling of null params.

Many solutions have offered the following workaround using native queries, see: https://www.baeldung.com/spring-data-jpa-null-parameters

@Query("SELECT c FROM Customer c WHERE (:name is null or c.name = :name) and (:email is null or c.email = :email)")
List<Customer> findCustomerByNameAndEmail(@Param("name") String name, @Param("email") String email);

However this still doesn't work in my case I get the following error when running a test

java.lang.IllegalArgumentException: Parameter of type String at index 0 in customer.name must not be null!

Is there something in the entity/repo that needs configured to prevent throwing the illegal args. Desired outcome would be "ignoring" of null value and just return me everything in that case.

I am aware of the following: https://jira.spring.io/browse/DATAJPA-209 still unresolved.

Rebecca Douglas
  • 429
  • 1
  • 5
  • 16

1 Answers1

1

Please try adding Nullable annotation -

@Query("SELECT c FROM Customer c WHERE (:name is null or c.name = :name) and (:email is null or c.email = :email)")
List<Customer> findCustomerByNameAndEmail(@Nullable @Param("name") String name, @Nullable @Param("email") String email);
SKumar
  • 1,940
  • 1
  • 7
  • 12