java 转换 Spring Data JPA 页面内容的类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32995179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:03:13  来源:igfitidea点击:

Convert type of Spring Data JPA Page content

javaspringhibernatespring-data-jpa

提问by Andrew Mairose

I am using Spring Data JPA and I have a PagingAndSortingRepository<Contact, Long>that uses a JPASpecificationExecutor<Contact>. I pass a Specificationand a Pageableinstance to the .findAll()method of this repository to get a Page<Contact>.

我正在使用 Spring Data JPA 并且我有一个PagingAndSortingRepository<Contact, Long>使用JPASpecificationExecutor<Contact>. 我将 aSpecification和一个Pageable实例传递给.findAll()此存储库的方法以获取Page<Contact>.

However, my Contactentity has a lot of extra fields and mappings that I don't need on my front end. So, I have a ContactDtothat contains only the necessary fields, and I have a method that can convert from Contactto ContactDto.

但是,我的Contact实体有很多我的前端不需要的额外字段和映射。所以,我有一个ContactDto只包含必要的字段,我也可以从一个转换方法ContactContactDto

private ContactDto convertToContactDto(Contact contact) {
    //do the conversion
}

How would I go about using this conversion method to convert the Page<Contact>to a Page<ContactDto>?

我将如何使用这种转换方法将 the 转换Page<Contact>为 a Page<ContactDto>

I can get the content of the Page<Contact>and do the conversion like this.

我可以获取内容Page<Contact>并像这样进行转换。

final Page<Contact> contactPage = pagingAndSortingContactRepository
        .findAll(ContactSpecification.findByFirstNmLike(firstNm), pageable);

final Collection<ContactDto> contactDtos = contactPage.getContent()
    .stream()
    .map(this::convertToContactDto)
    .collect(Collectors.toList());

But then I am left with a Collectioninstead of a Page, and I don't know how to get that Collectioninto the content of the Page. Is there a way to do this? Or is there another way to call the conversion on the Page<Contact>instance itself?

但随后我只剩下 aCollection而不是 a Page,我不知道如何将其Collection纳入Page. 有没有办法做到这一点?还是有另一种方法可以在Page<Contact>实例本身上调用转换?

回答by Andrew Mairose

Turns out that Pagehas its own .map()method, to which you can pass a method reference to do the conversion.

原来它Page有自己的.map()方法,你可以传递一个方法引用来进行转换。

Here is how I ended up doing the conversion.

这是我最终进行转换的方式。

final Page<ContactDto> contactDtoPage = contactPage.map(this::convertToContactDto);

The convertToContactDtomethod simply creates and returns an instance of the class I'm trying to convert to:

convertToContactDto方法只是创建并返回我试图转换为的类的一个实例:

private ContactDto convertToContactDto(final Contact contact) {
    final ContactDto contactDto = new ContactDto();
    //get values from contact entity and set them in contactDto
    //e.g. contactDto.setContactId(contact.getContactId());
    return contactDto;
}

回答by spaceman spiff

It may be the case that a Page transformation is less efficient to perform iteratively, as Page.map(..)is likely to do, than with the entire collection in hand.

Page.map(..)与手头的整个集合相比,页面转换迭代执行的效率可能较低,这很可能会发生。

In this case we can use Spring's PageExecutionUtilsto do the messy work of reconstructing a page with the transformed content.

在这种情况下,我们可以使用 SpringPageExecutionUtils来完成用转换后的内容重建页面的繁琐工作。

public Page<TypeB> getPageAsTypeB(Pageable pageable) {
    Page<TypeA> pageA = pagingAndSortingContactRepository(pageable);
    Function<List<TypeA>, List<TypeB>> collectionTransform;

    Page<TypeB> pageB = PageableExecutionUtils.getPage(
        collectionTransform.apply(pageA.getContent()),
        pageable,
        pageA::getTotalElements);
    return pageB;
}