Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jpa query with optional parameters

I m using Spring Data Rest JPA which implements the Query internally on the basis of method name.

I wrote the following method in my repository interface which should list all the users in a state and if the name and/or age is present, it should filter the result.

StateId is mandatory but name and age are optional filter parameters

public List<User> findByStateIdAndNameOrAge(Integer stateId, String name , Integer age, Pageable pageable);

I am not getting any results. Where am I doing wrong?

like image 946
zilcuanu Avatar asked Nov 07 '22 20:11

zilcuanu


1 Answers

You can try

There is no mistake in your method defination.

public List<User> findByStateIdAndNameOrAge(Integer stateId, String name , Integer age, Pageable pageable);

but you can't pass null parameter to this method so it will not work if you are putting any parameter is blank.

like image 157
Amit Gujarathi Avatar answered Nov 14 '22 22:11

Amit Gujarathi