I am using Spring Data JPA and I have a PagingAndSortingRepository<Contact, Long>
that uses a JPASpecificationExecutor<Contact>
. I pass a Specification
and a Pageable
instance to the .findAll()
method of this repository to get a Page<Contact>
.
However, my Contact
entity has a lot of extra fields and mappings that I don't need on my front end. So, I have a ContactDto
that contains only the necessary fields, and I have a method that can convert from Contact
to ContactDto
.
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>
?
I can get the content of the Page<Contact>
and do the conversion like this.
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 Collection
instead of a Page
, and I don't know how to get that Collection
into 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?
Its findById method retrieves an entity by its id. The return value is Optional<T> . Optional<T> is a container object which may or may not contain a non-null value. If a value is present, isPresent returns true and get returns the value.
Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.
PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.
Spring data jpa- it is same like jpa means we can describe in below way. Spring Data Jpa is jpa data abstraction access which means it likes a jpa but it add some extra functionality, Without jpa we can not implement the spring data jpa.
Turns out that Page
has its own .map()
method, to which you can pass a method reference to do the conversion.
Here is how I ended up doing the conversion.
final Page<ContactDto> contactDtoPage = contactPage.map(this::convertToContactDto);
The convertToContactDto
method simply creates and returns an instance of the class I'm trying to convert to:
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; }
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.
In this case we can use Spring's PageExecutionUtils
to do the messy work of reconstructing a page with the transformed content.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With