Azhenot
Azhenot

Reputation: 65

RepositoryItemReader doesn't find methods with arguments

I'm setting up an ItemRepositoryReader for the reader in a springBatch step.

public ItemReader<EcheanceEntity> reader(){
    RepositoryItemReader<EcheanceEntity> reader = new RepositoryItemReader<EcheanceEntity>();
    reader.setRepository(echeanceRepository);
    reader.setMethodName("findById");
    List parameters = new ArrayList();
    long a = 0;
    parameters.add(a);
    reader.setArguments(parameters);
    Map<String, Direction> sort = new HashMap<String, Direction>();
    sort.put("id", Direction.ASC);
    reader.setSort(sort);
    return reader;
}

this is the line in my repository.

public interface EcheanceRepository extends JpaRepository<EcheanceEntity, Long>{


public EcheanceEntity findById(long id);

@Override
public List<EcheanceEntity> findAll();

If a use the method findAll(), so without any arguments, it works fine. But if I use the method findById(long id) I get "no such method exception, findById(java.lang.Long, org.springframework.data.domain.PageRequest)" from the ItemRepositoryReader. The method works fine without using the reader when I'm testing it by using immediately the repository.

Thank you.

Upvotes: 6

Views: 16084

Answers (1)

Ilya Lysenko
Ilya Lysenko

Reputation: 1892

In a case of using the RepositoryItemReader#setMethodName method you need to add an argument of type Pageable in your repository method signature at last position:

    public interface EcheanceRepository extends JpaRepository<EcheanceEntity, Long> {
    
        public Page<EcheanceEntity> findById(long id, Pageable pageable);
        ...

You can find description of it in the documentation: http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/item/data/RepositoryItemReader.html#setMethodName-java.lang.String-

public void setMethodName(java.lang.String methodName)

Specifies what method on the repository to call. This method must take Pageable as the last argument.

Upvotes: 15

Related Questions