spring-batch

RepositoryItemReader - How can I use a method with List parameter


I have the following repository:

@Repository
public interface InterlocutorRepository extends PagingAndSortingRepository<Interlocutor, UsuarioId> {
    Page<Interlocutor> findByFlCadastroOnlineIn(List<StatusCadOnline> flCadastroOnline, Pageable pageable);
}

And I have the following reader in my Spring Batch App:

@Bean
    public ItemReader<Interlocutor> reader() {
        List<StatusCadOnline> listaStatusCadOnline = new ArrayList();
        listaStatusCadOnline.add(StatusCadOnline.PENDENTE);
        listaStatusCadOnline.add(StatusCadOnline.ERRO);

        RepositoryItemReader<Interlocutor> reader = new RepositoryItemReader<>();
        reader.setRepository(repository);
        reader.setMethodName("findByFlCadastroOnlineIn");
        reader.setArguments(listaStatusCadOnline);

        HashMap<String, Sort.Direction> sorts = new HashMap<>();
        sorts.put("nrCpf", Sort.Direction.ASC);
        reader.setSort(sorts);

        return reader;
    }

Spring Batch is understanding that my method has two arguments of type StatusCadOnline instead of a single parameter of type List.

LogError:

ERROR [SimpleAsyncTaskExecutor-178] b.c.a.c.b.steps.ItemSkipPolicy.shouldSkip 12 - java.lang.NoSuchMethodException: com.sun.proxy.$Proxy84.findByFlCadastroOnlineIn(br.com.alelo.corp.core.model.enumerator.StatusCadOnline, br.com.alelo.corp.core.model.enumerator.StatusCadOnline, org.springframework.data.domain.PageRequest)

Could anyone help me with that?

Thanks


Solution

  • To use the JPA's findBy*In() with RepositoryItemReader:

    You'll need to receive a list of arguments, and to do that you have to change your attributes to a List<List<?>>

    Example of running code solution to your case:

    ...
    List<List<StatusCadOnline>> arguments = new ArrayList();
    List<StatusCadOnline> attributeListIn = new ArrayList();
    attributeListIn.add(StatusCadOnline.PENDENTE);
    attributeListIn.add(StatusCadOnline.ERRO);
    arguments.add(attributeListIn);
        
    RepositoryItemReader<Filial> reader = new RepositoryItemReader<>();
    reader.setRepository(branchRepository);
    reader.setMethodName("findByFlCadastroOnlineIn");
    reader.setArguments(arguments);
    ...