分页dto春季

时间:2019-05-01 20:54:40

标签: java spring pagination

我想在dto类上使用可分页的界面(对元素进行排序),其中包含两个类模型的元素。不幸的是,当我使用存储库中的对象时,一切正常,但是当我使用第二类进行排序时,它不起作用。我该如何解决这个问题?

@RepositoryRestResource
public interface TopicRepository extends JpaRepository<Topic, Long> {
    Page<Topic> getTopicsByCategoryId(Long id, Pageable pageable);

使用主题元素时,一切正常。

 public Page<TopicPaginationDto> getPaginationTopics(Long id, Pageable pageable){
        Page<Topic> topics = topicRepository.getTopicsByCategoryId(id, pageable);

        Page<TopicPaginationDto> topicPaginationDtos =
                topics.map((Function<Topic, TopicPaginationDto>) topic -> new TopicPaginationDto( topic.getId(),
               topic.getTitle(), topic.getTopicAuthor().getUsername(),
               newestPost(topic.getId()).getPostAuthor().getUsername(),
               topic.getDisplayed(), topic.isPinned(), topic.getPosts().size(),
               newestPost(topic.getId()).getPostCreatedDate(),
               topic.getTopicCreatedDate())
return topicPaginationDtos;
        );

 return new PageImpl<>(topics.stream().map(topic -> new TopicPaginationDto(
                topic.getId(),
                topic.getTitle(), topic.getTopicAuthor().getUsername(),
                newestPost(topic.getId()).getPostAuthor().getUsername(),
                topic.getDisplayed(), topic.isPinned(), topic.getPosts().size(),
                newestPost(topic.getId()).getPostCreatedDate(),
                topic.getTopicCreatedDate()
        )).collect(Collectors.toList()), pageable, topics.getTotalElements());
        return topicPaginationDtos;
    }

当我尝试排序(在邮递员中)时,posts元素不起作用。

0 个答案:

没有答案