Home > Blockchain >  Spring : how to map Page<Entity> to Page<Object>
Spring : how to map Page<Entity> to Page<Object>

Time:07-25

I have a Repositoy (StockRepository ) class which extends from JpaRepository. When calling the method findAll(Pageable pageable); the return result is Page<Stock>.

Now I want to convert the Page<Stock> to Page<StockDto>. But the compiler give the error below. How can I correct this Compile error ?

import org.springframework.core.convert.converter.Converter;
...
    public Page<StockDto> getAllStocksPageable(int pageNumber, int pageSize) {
    
        Pageable pageable = PageRequest.of(pageNumber, pageSize);
        Page<Stock> stocks = stockRepository.findAll(pageable);
    
        Page<StockDto> dtoPage = stocks.map(new Converter<Stock, StockDto>() {
            @Override
            public StockDto convert(Stock entity) {
                StockDto stockDto = new StockDto();
                ...
                return stockDto;
            }
    
        });
    
        return null;
    
    }

enter image description here

@Entity
@Table(name = "stock")
public class Stock {
        ....
}


// Pojo
public class StockDto {
    ....
 }


@Repository
public interface StockRepository extends JpaRepository<Stock, Integer> {
    ....
}

CodePudding user response:

I would like to suggest a slightly different approach, create a data projection and let the Repository just return a page of the DTO for you. Here's an example assuming that your DTO contains a name and id field.

public interface StockDTOView {
   public getName();
   public getId();
}

The repository method would be written like this:

Page<StockDTOView> findAllProjectedBy(Pageable pageable);

When you call this method on your repository, you'll get back a page of the projection, there's no need to do any conversions.

CodePudding user response:

The error is pretty clear: Page.map() accepts a Function, but you've given it a Converter. Replace new Converter( with new Function( and convert( with apply( and it'll compile:

Page<StockDto> dtoPage = stocks.map(new Function<Stock, StockDto>() {
    @Override
    public StockDto apply(Stock entity) {
        StockDto stockDto = new StockDto();
        ...
        return stockDto;
    }
});

Better yet, use a lambda:

Page<StockDto> dtoPage = stocks.map(entity -> {
        StockDto stockDto = new StockDto();
        ...
        return stockDto;
    });

CodePudding user response:

I see you are using a Converter coming from Spring (does not implement Function). If you use the one in guava 27.0, the Converter abstract class (implements Function interface) has 2 abstract methods to implement:

import com.google.common.base.Converter;
//...

    Page<StockDto> dtoPage = stocks.map(new Converter<Stock, StockDto>() {
        @Override
        protected StockDto doForward(final Stock stock) {
            StockDto stockDto = new StockDto();
            // ...
            return stockDto;
        }

        @Override
        protected Stock doBackward(final StockDto dto) {
            // probably you don't need this, can just return a null,
            return null;
        }
    });
  • Related