Return page object from JPA query

Peter Penzov :

I want to create JPA with pagination. I tried to implement this:

@Override
    public Page<PaymentTransactions> findAll(Specification<PaymentTransactions> spec, Pageable pageable) {

        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();      

        String hql = "select e from " + PaymentTransactions.class.getName() + " e LIMIT :limit OFFSET :offset";
        TypedQuery<PaymentTransactions> query = entityManager.createQuery(hql, PaymentTransactions.class).setParameter("limit", pageSize).setParameter("offset", pageNumber);
        Page<PaymentTransactions> paymentTransactions = (Page<PaymentTransactions>) query.getResultList();
        return paymentTransactions;
    }

How I can return Page Object without using casting?

EDIT I also tried this:

End point:

@Autowired
private PaymentTransactionRepository transactionRepository;

@GetMapping
public Page<PaymentTransactionsDTO> page(@PathVariable int page, @PathVariable int size) {
        return transactionRepository
                .findAll(page, size)
                .map(mapper::toDTO);
    }

Repository:

public interface PaymentTransactionRepository extends CrudRepository<PaymentTransactions, Integer>, JpaSpecificationExecutor<PaymentTransactions> {

    @Query(nativeQuery=true, 
            value="SELECT * FROM payment_transactions \n-- #pageable\n",
            countQuery="SELECT count(*) FROM payment_transactions")
    Page<PaymentTransactions> findAll(Pageable page);
}

EDIT 2. I also tried this:

@GetMapping("page")
    public Page<PaymentTransactionsDTO> page(@PathVariable int page, @PathVariable int size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        return transactionRepository.findAll(pageRequest).map(mapper::toDTO);
    }

Interface:

public interface PaymentTransactionRepository extends CrudRepository<PaymentTransactions, Integer>, JpaSpecificationExecutor<PaymentTransactions> {

    Page<PaymentTransactions> findAll(Pageable page);
}

Implementation:

@Override
    public Page<PaymentTransactions> findAll(Pageable page) {
        int pageNumber = page.getPageNumber();
        int pageSize = page.getPageSize();

        String hql = "select e from " + PaymentTransactions.class.getName() + " e LIMIT :limit OFFSET :offset";
        TypedQuery<PaymentTransactions> query = entityManager.createQuery(hql, PaymentTransactions.class)
                .setParameter("limit", pageSize).setParameter("offset", pageNumber);
        Page<PaymentTransactions> paymentTransactions = (Page<PaymentTransactions>) query.getResultList();
        return paymentTransactions;
    }
Vivek Kurmi :

To get the functionality of Paging and Sorting we have to extend either PagingAndSortingRepository or JpaRepository interfaces.

In your case we can achieve the same by below code:

End Point:

@Autowired
private PaymentTransactionRepository transactionRepository;

@GetMapping
public Page<PaymentTransactionsDTO> page(@PathVariable int page, @PathVariable int size) {
    PageRequest pageRequest = PageRequest.of(page, size);
    return transactionRepository
            .findAll(pageRequest)
            .map(mapper::toDTO);
}

Repository:

public interface PaymentTransactionRepository extends JpaRepository<PaymentTransactions, Integer> {
    Page<PaymentTransactions> findAll(Pageable page);
}

Please try with this, it should work.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=82970&siteId=1