0

I am trying to implement the service for a rest controller that takes optional filter parameters like first name, last name, age, etc. I want to use a JpaRepository method like below. The problem is, if a parameter is null then no records are found.

When a argument is null i do not want filtering to occur on that field. Is there a way to implement this out of the box with JpaRepository?

Page<Customer> findByLastNameAndByFirstNameAndByAge(String lastName, String firstName, int age, Pageable pageable); 

1 Answer 1

1

From the spring docs Table 2.2 indicates you could use something similar to findByLastnameOrFirstnameOrAge, which would ignore the null values.

IF that doesn't get what you want, you'll have to do it with a Specification (see Section 3 of the above document).

Sign up to request clarification or add additional context in comments.

1 Comment

The Or will not work since i need a combination of the filters, but it looks like Specification will do the trick. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.