Ivan
Ivan

Reputation: 180

Incompatible types List and Page

I am developing a webapp using JHipster that manages payments and wanted to filter the payments that a user can see so he can only see his payments. To do this I was following the blog tutorial from Matt Raible on Youtube. He uses findByUserIsCurrentUser() which is generated by JHipster.

List<Blog> blogs = blogRepository.findByUserIsCurrentUser();

When I went to make the same change in my project I found the type that I must return is a Page and I get an incompatible type error, here is my method:

public Page<Payment> findAll(Pageable pageable) {
    log.debug("Request to get all Payments");
    Page<Payment> result = paymentRepository.findByUserIsCurrentUser();
    return result;
}

If I change the findAll(pageable) for findByUserIsCurrentUser() which is declared in the PaymentRepository as follows

public interface PaymentRepository extends JpaRepository<Payment,Long> {
    @Query("select payment from Payment payment where payment.user.login = ?#{principal.username}")
    List<Payment> findByUserIsCurrentUser();
}

I get the following error:

Error

How can I solve this?

Upvotes: 3

Views: 1410

Answers (1)

geraldhumphries
geraldhumphries

Reputation: 1493

You can fix this in two ways. This is assuming you are using a service, if not it's still pretty similar:

Method 1: If you want your service to return ALL payments for the current user, then you need to modify your service and probably your resource to handle a list instead of a page:

public List<Payment> findAll() {
    log.debug("Request to get all Payments");
    List<Payment> result = paymentRepository.findByUserIsCurrentUser();
    return result;
}

And in your resource:

public List<Payment> getAllPayments() {
    log.debug("REST request to get all Payments");
    List<Payment> payments = paymentService.findAll();
    return payments;
}

Alternatively, you can change "pagination": "pagination" to "pagination": "no" in .jhipster/Payment.json and rerun the generator. This will regenerate the service, resource and repository without pagination.

Method 2: If you want your service to be paginated, you need to change your repository method to accept the Pageable object, and return a page:

public interface PaymentRepository extends JpaRepository<Payment,Long> {
    @Query("select payment from Payment payment where payment.user.login = ?#{principal.username}")
    Page<Payment> findByUserIsCurrentUser(Pageable pageable);
}

Then you need to pass the pageable object in from your service:

public Page<Payment> findAll(Pageable pageable) {
    log.debug("Request to get all Payments");
    Page<Payment> result = paymentRepository.findByUserIsCurrentUser(pageable);
    return result;
}

This will return a subset of results, based on whatever the pageable object contains. This includes size, page number, offset, and sorting. For example, a size of 20 and a page number of 0 will return the first 20 results. Changing the page number to 1 will return the next 20 results. These parameters are generally passed in from the front end to your API - you will notice that getAllPayments() in PaymentResource has a Pageable parameter.

Upvotes: 4

Related Questions