Search code examples
javahibernatejpaspring-data-jpaenumeration

How to findAll by multplie string values?


I have an entity employee which has status field as @Enumerated which is being saved as String in PostgreSQL.

.

status can contain 3 values: ACTIVE, INACTIVE, TRIAL.

I want a JPA method without named query, or @Query Annotation which will fetch me all Employees where status = 'ACTIVE' or 'TRIAL'. .

`findAllByStatusIn(List statusList)' gives only the last added status to the list.

i.e., if I add

statusList.add(Status.ACTIVE)
statusList.add(Status.TRIAL)

. then I only get TRIAL employees . But if I add

statusList.add(Status.TRIAL);
statusList.add(Status.ACTIVE);

How do I get values with both statuses using JPA method?

Any help is appreciated.

New to JPA and Spring data.

Please guide me to the right answer if this question is a possible duplicate.

Thanks in advance.


Solution

  • After trying a few versions from the above valuable comments, I found the right method for fetching based on a list of statuses.

    Page<Employee> findAllByStatusIn(List<Status> statusList, Pageable pageable);
    

    works as expected. Gives pageable info as well. I also tried by removing All keyword from the method.

    This too works the same:

    Page<Employee> findByStatusIn(List<Status> statusList, Pageable pageable);
    

    Since it is multiple records fetched at once, I kept the All keyword for better understanding.

    But I believe it is not really necessary.