How to handle all enum value in JPA Query method for empty parameter

Anil :

I have a JPA method that finds list of Students by their graduation status.

List<Student> findAllByStatus(Status status);

But if the request is made with null Status, I want to retrieve entities with null status.

How can I handle this with JPARepository using only one method query, to achieve no filtering by status if the status is null?

Thanks in advance.

Raniere :

You should use something like:

@Query("SELECT s from Student s WHERE (?1 is null or s.status = ?1)")
List<Student> findAllByStatus(Status status);

Just a little fix to Ankit Kanani answer.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=80356&siteId=1