ModelMapper - A mapping already exists - Two configurations
Asked Answered
E

2

0

I have two classes that use ModelMapper to convert entity to DTO. In each class I have a configuration in class's constructor to ModelMapper, to avoid convert all the relations into DTO and get StackOverflowError.

CompanyServiceImpl

private ModelMapper modelMapper;

@Autowired
public CompanyServiceImpl(ModelMapper modelMapper) {
    this.modelMapper = modelMapper;

    modelMapper.addMappings(skipCompanyServiceFieldsMap);
    modelMapper.addMappings(skipCompanyServiceModuleFieldsMap);
}

PropertyMap<CompanyServiceModule, CompanyServiceModuleDTO> skipCompanyServiceModuleFieldsMap = new PropertyMap<CompanyServiceModule, CompanyServiceModuleDTO>() {
    @Override
    protected void configure() {
        // Tells ModelMapper to NOT populate back references
        skip(destination.getCompanyService());
        skip(destination.getServiceModule().getCompanyServiceModules());
        skip(destination.getServiceModule().getService());
        skip(destination.getServiceModule().getServiceModuleLanguages());
        skip(destination.getServiceModule().getServiceModuleMenus());
    }
};

CompanyProfileImpl

private ModelMapper modelMapper;

@Autowired
public CompanyProfileImpl(ModelMapper modelMapper) {
    this.modelMapper = modelMapper;

    modelMapper.addMappings(skipCompanyProfileFieldsMap);
    modelMapper.addMappings(skipCompanyProfileModuleFieldsMap);
    modelMapper.addMappings(skipCompanyProfileServiceModuleFieldsMap);
}

PropertyMap<CompanyServiceModule, CompanyServiceModuleDTO> skipCompanyProfileServiceModuleFieldsMap = new PropertyMap<CompanyServiceModule, CompanyServiceModuleDTO>() {
    @Override
    protected void configure() {
        // Tells ModelMapper to NOT populate back references
        skip(destination.getCompanyProfileModules());
        skip(destination.getServiceModule());
        skip(destination.getCompanyService());
    }
};

When I run the application I'm getting the error:

A mapping already exists for com.closeupinternational.authorization.dtos.CompanyServiceModuleDTO.setCompanyService().

How can I have two configuration for ModelMapper, each one specific to the implemented service? Since in one case makes sense to bring some relations and in the other not.

Erasure answered 16/12, 2020 at 19:45 Comment(2)
How and where is ModelMapper initialized?Exogamy
I've changed but I'd like to know if someone has a better solution. @ExogamyAmbrosial
E
1

I changed the instantiation of the class into constructor and not leave Spring to instantiate and treat as Singleton.

private ModelMapper modelMapper;

public CompanyProfileImpl() {
    // Necessary to not get in conflict with others
    this.modelMapper = new ModelMapper();

    modelMapper.addMappings(skipCompanyProfileFieldsMap);
    modelMapper.addMappings(skipCompanyProfileModuleFieldsMap);
    modelMapper.addMappings(skipCompanyServiceModuleFieldsMap);
}

Erasure answered 17/12, 2020 at 18:54 Comment(0)
P
0

I have just solved it by moving all configuration to the @Bean instance in @AppConfig. Leaving MyMapper.class with only return of myMapper.map() statement.

Pravit answered 29/5, 2022 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.