1 year ago

#381937

test-img

Code Alpha

Mapstruct: Mapping a dto in a dto to an entity in an entity

Mapstruct is not using a mapper declared in the "uses" definition in the @Mapper Annotation I expected the productCategory and productPrices to be mapped by the mappers: ProductPricesMapper.class, ProductCategoryMapper.class that are declared below but the generated code does not have them

ProductMapper Interface

@Mapper(componentModel = "spring", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE, uses = { ``
ProductPricesMapper.class, ProductCategoryMapper.class })`
public interface ProductMapper {`

    @Mapping(target = "lastUpdatedTime", ignore = true)
    @Mapping(target = "creationTime", ignore = true)
    Product createEntityFromDto(ProductDto productDto);
    
}

Generated Code:

    @Autowired
    private ProductPricesMapper productPricesMapper;
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    
    
    
    @Override
    public Product createEntityFromDto(ProductDto productDto) {
        if ( productDto == null ) {
            return null;
        }
    
        Product product = new Product();
    
        product.setDescription( productDto.getDescription() );
        product.setId( productDto.getId() );
        product.setName( productDto.getName() );

        product.setProductCategory( productCategoryDtoToProductCategory( 
       productDto.getProductCategory() ) );
product.setProductPrices( productPricesDtoToProductPrices( productDto.getProductPrices() ) );

        return product;
    }

Expected Generated Code

@Component
public class ProductMapperImpl implements ProductMapper {

    @Autowired
    private ProductPricesMapper productPricesMapper;
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    
    
    
    @Override
    public Product createEntityFromDto(ProductDto productDto) {
        if ( productDto == null ) {
            return null;
        }
    
        Product product = new Product();
    
        product.setDescription( productDto.getDescription() );
        product.setId( productDto.getId() );
        product.setName( productDto.getName() );

       product.setProductCategory(productCategoryMapper.createEntityFromDto( productDto.getProductCategory() ));
product.setProductPrices( productPricesMapper.createEntityFromDto(productDto.getProductPrices()));
return product;

 }

CODE FOR PRODUCT CATEGORY DTO TO PRODUCT CATEGORY

 protected ProductCategory productCategoryDtoToProductCategory(ProductCategoryDto productCategoryDto) {
        if ( productCategoryDto == null ) {
            return null;
        }

        ProductCategory productCategory = new ProductCategory();

        productCategory.setDescription( productCategoryDto.getDescription() );
        productCategory.setId( productCategoryDto.getId() );
        productCategory.setName( productCategoryDto.getName() );

        return productCategory;
    }

CODE FOR PRODUCT PRICES DTO TO PRODUCT PRICES


    protected ProductPrices productPricesDtoToProductPrices(ProductPricesDto productPricesDto) {
        if ( productPricesDto == null ) {
            return null;
        }

        ProductPrices productPrices = new ProductPrices();

        productPrices.setBuyQuantityOffer( productPricesDto.getBuyQuantityOffer() );
        productPrices.setDiscount( productPricesDto.getDiscount() );
        productPrices.setGetQuantityOffer( productPricesDto.getGetQuantityOffer() );
        productPrices.setGetQuantityOfferProduct( productDtoToProduct( productPricesDto.getGetQuantityOfferProduct() ) );
        productPrices.setId( productPricesDto.getId() );
        productPrices.setSellingPrice( productPricesDto.getSellingPrice() );

        return productPrices;
    }

ANOTHER SCENARIO

when I have the following code in the ProductMapper but now it is vice versa the generated code is okay, so what is making the previous fail?

CODE ADDED TO PRODUCT MAPPER

ProductDto createDtoFromEntity(Product product);

CORRECT GENERATED CODE

@Override
    public ProductDto createDtoFromEntity(Product product) {
        if ( product == null ) {
            return null;
        }

        ProductDto productDto = new ProductDto();

        productDto.setDescription( product.getDescription() );
        productDto.setId( product.getId() );
        productDto.setName( product.getName() );
        productDto.setProductCategory( productCategoryMapper.createDtoFromEntity( product.getProductCategory() ) );
        productDto.setProductPrices( productPricesMapper.createDtoFromEntity( product.getProductPrices() ) );

        return productDto;
    }

java

spring-boot

dto

mapstruct

0 Answers

Your Answer

Accepted video resources