user14528173
user14528173

Reputation:

Pagination & Specification usage Spring Data

I am trying to add pagination to my transactions

This below soltuion works perfectly fine

List<AccountTransactions> products = accountTransactionRepository.findAll(specification);

int skip = (page - 1) * ps;

List<AccountTransactionDto> transactionDtos =
        AccountTransactionMapper.toAccountTransactionDtoList(products).stream()
                .skip(skip).limit(ps).collect(Collectors.toList());

But i got tried to do by using spring data as regarding suggestion in first post. Another reason is i think usign stream causes performance issues.

Repository class

@Repository
public interface AccountTransactionRepository extends PagingAndSortingRepository<AccountTransactions, Integer>,
        JpaSpecificationExecutor<AccountTransactions> {
}

ServiceImpl. class

Pageable pageable = PageRequest.of(1, 5);

Page<List<AccountTransactions>> products = accountTransactionRepository.findAll(specification,pageable);

But this returns an error

Required type:
List
<AccountTransactions>
Provided:
Page
<AccountTransactions>

I think i need to return Page<List> as return type.

When i convert it to Page it works but returns the wrong result.

Also do you have any idea what is the best way as regarding the performance point of view?

Upvotes: 1

Views: 679

Answers (1)

Steephen
Steephen

Reputation: 15824

findAll by default returns Page instance. So you have to correct following line:

From

Page<List<AccountTransactions>> products = accountTransactionRepository.findAll(specification,pageable);

To

Page<AccountTransactions> products = accountTransactionRepository.findAll(pageable);

Upvotes: 1

Related Questions