0

I have a spring boot application with @RestController and within that I got a @GetMapping Method, in which I return a List of DTOs from a Native Query to my Client. Now I wanted to add pagination.

But since my Method is not returning an entity but an DTO, which contains fields of three related entities it makes it difficult for me to find an proper way to do this. I have already tried some approaches of people with similar problems but none of those were helping me and a lot of them are deprecated I think.

So my Question is: How can I return a Page of type DTO to my frontend? The Native Query returns a List of type DTO.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

1

since you are using a native query, you can make a frontend to return a pageIndex and pageSize, for example for 1st set of results pageIndex = 0 and pageSize = 50 and for next set pageIndex = 1 and pageSize = 50. You can use the below native query for it.

SELECT name ,address FROM users OFFSET (pageIndex * pageSize) ROWS FETCH NEXT pageSize ROWS ONLY

root
  • 11
  • 4
  • Hey, thanks for the answer! I changed my Query from Native to JPQL and I am now using a Custom Repository Impl to implement my queries. Could you tell me how to implement an JPQL equivalent of what I was asking about before? I would also be fine with just returning a paginated/limited List of Type Object, but it can't be a Page of Type Object because I am using the DTO constructor to convert the Object after fetching it from the database and then expose the DTO in the API. I hope you can make any sense of what I'm trying to explain here. – Uniqueusername Feb 08 '21 at 00:23