javaspring-data

Error that 2 parameter(s) provided but only 1 parameter(s) present in query


I have an error and i dont know how to fix it. I see other similar issues here, but they haven't been useful to me. So, I hope you could help me.

I have this DAO:

public interface GoalDao extends PagingAndSortingRepository<Goal, Long> {

    Slice<Goal> findByCompanyId(Long companyId, PageRequest of);

}

And this method in my service:

    @Override
    @Transactional(readOnly = true)
    public Block<Goal> findCompanyGoals(Long userId, Long companyId, int page, int size)
            throws InstanceNotFoundException, PermissionException {
        Company company = permissionChecker.checkCompanyExistsAndBelongsToUser(companyId, userId);

        Slice<Goal> slice = goalDao.findByCompanyIdOrderByIdDesc(company.getId(), PageRequest.of(page, size));
        return new Block<>(slice.getContent(), slice.hasNext());
    }

So, this line:

    Slice<Goal> slice = goalDao.findByCompanyIdOrderByIdDesc(company.getId(), PageRequest.of(page, size));

It is throwing me the following error:

testException = org.springframework.dao.InvalidDataAccessApiUsageException: At least 2 parameter(s) provided but only 1 parameter(s) present in query.; nested exception is java.lang.IllegalArgumentException: At least 2 parameter(s) provided but only 1 parameter(s) present in query.

Solution

  • In your GoalDao try using Pageable instead of PageRequest. So it will look like this:

    public interface GoalDao extends PagingAndSortingRepository<Goal, Long> {
    
        Slice<Goal> findByCompanyId(Long companyId, Pageable pageable);
    
    }
    

    Not sure why, but it worked for me and I hope it will help you too.