简体   繁体   English

Spring Data Pagination返回错误的内容数量

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

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. 目前,我正在开发一个使用Spring Data Pagination的api,并且遇到一个问题,即我将一个Pageable对象作为请求传递给我的存储库,我希望使用该页面接收哪个页面以及应该包含多少个元素。 And with that my object looks like: Pageable pageable = PageRequest.of(0, 2); 我的对象如下所示: 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. 在我的数据库中有3个对象,因此将有2页。 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? 谁能告诉我为什么它表明内容是3个元素的数组,但实际上我要2个元素?

   @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());
}

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM