Spring Data Pagination returns wrong number of content as it is requested

Multi tool use
currently I am developing an api which uses Spring Data Pagination
and I came to the problem that I passing a Pageable
object as a request to my repository with which page I want to receive and how many elements should be on it. And with that my object looks like: Pageable pageable = PageRequest.of(0, 2);
- so I want first page with two elements. In my database are 3 objects so therefore there will be 2 pages. But what ide shows me is: screenshot. Can anyone tell me why it shows that content is an array of 3 elements but actually I asked for 2 elements?
@Override
public List<NotificationDto> getLast30NotificationsFor(ApplicationUser user) {
Pageable pageable = PageRequest.of(0, 2);
Page<Notification> first30ByOwnerIdOrderByCreatedAtDesc = notificationRepository.findFirst30ByOwnerIdOrderByCreatedAtDesc(user.getId(), pageable);
List<Notification> notifications = new ArrayList<>(first30ByOwnerIdOrderByCreatedAtDesc.getContent());
return notifications.stream()
.map(NotificationToDtoMapper::map)
.collect(toList());
}
java spring pagination spring-data-jpa
|
show 4 more comments
currently I am developing an api which uses Spring Data Pagination
and I came to the problem that I passing a Pageable
object as a request to my repository with which page I want to receive and how many elements should be on it. And with that my object looks like: Pageable pageable = PageRequest.of(0, 2);
- so I want first page with two elements. In my database are 3 objects so therefore there will be 2 pages. But what ide shows me is: screenshot. Can anyone tell me why it shows that content is an array of 3 elements but actually I asked for 2 elements?
@Override
public List<NotificationDto> getLast30NotificationsFor(ApplicationUser user) {
Pageable pageable = PageRequest.of(0, 2);
Page<Notification> first30ByOwnerIdOrderByCreatedAtDesc = notificationRepository.findFirst30ByOwnerIdOrderByCreatedAtDesc(user.getId(), pageable);
List<Notification> notifications = new ArrayList<>(first30ByOwnerIdOrderByCreatedAtDesc.getContent());
return notifications.stream()
.map(NotificationToDtoMapper::map)
.collect(toList());
}
java spring pagination spring-data-jpa
and size is 2 is pageable request...as you see in screenshot I asked for 2, not for 3...
– Jan Testowy
Dec 30 '18 at 12:23
Your pageable is useless. Your query method is converter into a query and it will always select the first 30. Hence the limit you give in your pageable is not taken into account.
– M. Deinum
Dec 30 '18 at 18:00
@M.Deinum why it is useless?
– Jan Testowy
Dec 30 '18 at 18:17
Because in your query you always select 30.
– M. Deinum
Dec 30 '18 at 18:21
@M.Deinum but why?
– Jan Testowy
Dec 30 '18 at 18:51
|
show 4 more comments
currently I am developing an api which uses Spring Data Pagination
and I came to the problem that I passing a Pageable
object as a request to my repository with which page I want to receive and how many elements should be on it. And with that my object looks like: Pageable pageable = PageRequest.of(0, 2);
- so I want first page with two elements. In my database are 3 objects so therefore there will be 2 pages. But what ide shows me is: screenshot. Can anyone tell me why it shows that content is an array of 3 elements but actually I asked for 2 elements?
@Override
public List<NotificationDto> getLast30NotificationsFor(ApplicationUser user) {
Pageable pageable = PageRequest.of(0, 2);
Page<Notification> first30ByOwnerIdOrderByCreatedAtDesc = notificationRepository.findFirst30ByOwnerIdOrderByCreatedAtDesc(user.getId(), pageable);
List<Notification> notifications = new ArrayList<>(first30ByOwnerIdOrderByCreatedAtDesc.getContent());
return notifications.stream()
.map(NotificationToDtoMapper::map)
.collect(toList());
}
java spring pagination spring-data-jpa
currently I am developing an api which uses Spring Data Pagination
and I came to the problem that I passing a Pageable
object as a request to my repository with which page I want to receive and how many elements should be on it. And with that my object looks like: Pageable pageable = PageRequest.of(0, 2);
- so I want first page with two elements. In my database are 3 objects so therefore there will be 2 pages. But what ide shows me is: screenshot. Can anyone tell me why it shows that content is an array of 3 elements but actually I asked for 2 elements?
@Override
public List<NotificationDto> getLast30NotificationsFor(ApplicationUser user) {
Pageable pageable = PageRequest.of(0, 2);
Page<Notification> first30ByOwnerIdOrderByCreatedAtDesc = notificationRepository.findFirst30ByOwnerIdOrderByCreatedAtDesc(user.getId(), pageable);
List<Notification> notifications = new ArrayList<>(first30ByOwnerIdOrderByCreatedAtDesc.getContent());
return notifications.stream()
.map(NotificationToDtoMapper::map)
.collect(toList());
}
java spring pagination spring-data-jpa
java spring pagination spring-data-jpa
asked Dec 30 '18 at 12:02
Jan TestowyJan Testowy
76211
76211
and size is 2 is pageable request...as you see in screenshot I asked for 2, not for 3...
– Jan Testowy
Dec 30 '18 at 12:23
Your pageable is useless. Your query method is converter into a query and it will always select the first 30. Hence the limit you give in your pageable is not taken into account.
– M. Deinum
Dec 30 '18 at 18:00
@M.Deinum why it is useless?
– Jan Testowy
Dec 30 '18 at 18:17
Because in your query you always select 30.
– M. Deinum
Dec 30 '18 at 18:21
@M.Deinum but why?
– Jan Testowy
Dec 30 '18 at 18:51
|
show 4 more comments
and size is 2 is pageable request...as you see in screenshot I asked for 2, not for 3...
– Jan Testowy
Dec 30 '18 at 12:23
Your pageable is useless. Your query method is converter into a query and it will always select the first 30. Hence the limit you give in your pageable is not taken into account.
– M. Deinum
Dec 30 '18 at 18:00
@M.Deinum why it is useless?
– Jan Testowy
Dec 30 '18 at 18:17
Because in your query you always select 30.
– M. Deinum
Dec 30 '18 at 18:21
@M.Deinum but why?
– Jan Testowy
Dec 30 '18 at 18:51
and size is 2 is pageable request...as you see in screenshot I asked for 2, not for 3...
– Jan Testowy
Dec 30 '18 at 12:23
and size is 2 is pageable request...as you see in screenshot I asked for 2, not for 3...
– Jan Testowy
Dec 30 '18 at 12:23
Your pageable is useless. Your query method is converter into a query and it will always select the first 30. Hence the limit you give in your pageable is not taken into account.
– M. Deinum
Dec 30 '18 at 18:00
Your pageable is useless. Your query method is converter into a query and it will always select the first 30. Hence the limit you give in your pageable is not taken into account.
– M. Deinum
Dec 30 '18 at 18:00
@M.Deinum why it is useless?
– Jan Testowy
Dec 30 '18 at 18:17
@M.Deinum why it is useless?
– Jan Testowy
Dec 30 '18 at 18:17
Because in your query you always select 30.
– M. Deinum
Dec 30 '18 at 18:21
Because in your query you always select 30.
– M. Deinum
Dec 30 '18 at 18:21
@M.Deinum but why?
– Jan Testowy
Dec 30 '18 at 18:51
@M.Deinum but why?
– Jan Testowy
Dec 30 '18 at 18:51
|
show 4 more comments
1 Answer
1
active
oldest
votes
Try:
@Override
public List<NotificationDto> getLast30NotificationsFor(ApplicationUser user) {
Pageable page = PageRequest.of(0,2, Sort.by("createdAt").descending());
return notificationRepository.findByOwnerId(user.getId(), page)
.getContent()
.stream()
.map(NotificationToDtoMapper::map)
.collect(toList());
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53977411%2fspring-data-pagination-returns-wrong-number-of-content-as-it-is-requested%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try:
@Override
public List<NotificationDto> getLast30NotificationsFor(ApplicationUser user) {
Pageable page = PageRequest.of(0,2, Sort.by("createdAt").descending());
return notificationRepository.findByOwnerId(user.getId(), page)
.getContent()
.stream()
.map(NotificationToDtoMapper::map)
.collect(toList());
}
add a comment |
Try:
@Override
public List<NotificationDto> getLast30NotificationsFor(ApplicationUser user) {
Pageable page = PageRequest.of(0,2, Sort.by("createdAt").descending());
return notificationRepository.findByOwnerId(user.getId(), page)
.getContent()
.stream()
.map(NotificationToDtoMapper::map)
.collect(toList());
}
add a comment |
Try:
@Override
public List<NotificationDto> getLast30NotificationsFor(ApplicationUser user) {
Pageable page = PageRequest.of(0,2, Sort.by("createdAt").descending());
return notificationRepository.findByOwnerId(user.getId(), page)
.getContent()
.stream()
.map(NotificationToDtoMapper::map)
.collect(toList());
}
Try:
@Override
public List<NotificationDto> getLast30NotificationsFor(ApplicationUser user) {
Pageable page = PageRequest.of(0,2, Sort.by("createdAt").descending());
return notificationRepository.findByOwnerId(user.getId(), page)
.getContent()
.stream()
.map(NotificationToDtoMapper::map)
.collect(toList());
}
answered Dec 30 '18 at 19:05
Robert NiestrojRobert Niestroj
7,80064579
7,80064579
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53977411%2fspring-data-pagination-returns-wrong-number-of-content-as-it-is-requested%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
E1nRSdIA62zkI4m DC68fi,kXCi9,RqGB
and size is 2 is pageable request...as you see in screenshot I asked for 2, not for 3...
– Jan Testowy
Dec 30 '18 at 12:23
Your pageable is useless. Your query method is converter into a query and it will always select the first 30. Hence the limit you give in your pageable is not taken into account.
– M. Deinum
Dec 30 '18 at 18:00
@M.Deinum why it is useless?
– Jan Testowy
Dec 30 '18 at 18:17
Because in your query you always select 30.
– M. Deinum
Dec 30 '18 at 18:21
@M.Deinum but why?
– Jan Testowy
Dec 30 '18 at 18:51