I've got an Entity class like this :
class EntityClass {
UUID id;
String param1, param2, param3;
//Getters and Setters
}
My Service class will've some method like this to make use of all the columns like this, and I want to be able to handle all requests with the same repository method instead of writing multiple repository methods for each case where a combination of parameters is null.
class ServiceClass {
List<EntityClass> getAllBy(List<String> param1, List<String> param2, String param3) {
return repositoryInterface.customFindAll(param1, param2, param3);
}
}
Now my repository class should've this method. But I want to be able to handle the case where any combination of {param1, param2, param3} can be null as in {param1 = null or empty, param2 != null, param3 != null} or {param1 = null or empty, param2 = null or empty, param3 != null} etc.
The repository method customFindAll should basically be able to search entity table to check if param1 column value is in any of the lists of values sent etc. if not null.
So how should I handle the native query in spring JPA to do such a thing instead of creating different methods for different combinations?
interface RepositoryInterface extends JpaRepository<EntityClass, UUID> {
@Query(value = "FILL THE QUERY", nativeQuery = true)
List<EntityClass> customFindAll(@Param("param1") List<String> param1, @Param("param2") List<String> param2, @Param("param3") String param3)
}
null