How to skip a field during map stage?
Asked Answered
M

3

5

I'm having list of employee objects - List I need to convert it into list of employee transfer objects - List

Assume a field "password" exist in both the classes.

  • In few cases i need the password needs to be included from Employee → EmployeeDTO
  • In few cases i don't need the password and want to be excluded from Employee - EmployeeDTO.

Sample Code Snippet:

    List<Employee> employees = employeeRepository.findAll();
    // Define the target type
    Type targetListType = new TypeToken<List<EmployeeDTO>>() {}.getType();
    List<EmployeeDTO> employeeDTOs = modelMapper.map(employees, targetListType);

Please let me know how to skip the fields on mapping/copying.

Michaud answered 8/6, 2016 at 9:28 Comment(0)
R
6

Take a look the official user manual of Conditional Mapping.

In brief:

You would need to add a new Mapping and use a Condition. Your source and destionation would be:

  • Source: Employee
  • Destination: EmployeeDto

First create and custom your Condition. It would be something like this:

Condition<?, ?> isNotZero = new Condition<PersonDTO, Employee>() {
    public boolean applies(MappingContext<PersonDTO, Employee> context) {
      //Your conidition
      return context.getSource().getEmployeeId() != 0;
    }
  };

Then add Mapping and use the condition:

modelMapper.addMappings(new PropertyMap<PersonDTO, Person>() {
      protected void configure() {
        when(isNotZero).map(source).setEmployee(null);
      }
    });

You can find this examples in the ModelMapper GitHub repository. The author has done few more and are well explained:

  • Link to above example
Rau answered 2/8, 2016 at 19:58 Comment(0)
I
4

Here is how I skip fields during the mapping stage:

    ModelMapper modelMapper = new ModelMapper();

    modelMapper.typeMap(EmployeeDTO.class,Employee.class).addMappings(mapper -> {
        mapper.skip(Employee::setPassword);
    });
Ickes answered 9/12, 2019 at 16:11 Comment(1)
Does this complie?Brooksbrookshire
O
0

Try to custom configure the ModelMapper.

I tried the below configuration. Hope you get this to know.

@Bean(name = "PasswordskipMapper")
    public  ModelMapper modelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

        // Create a PropertyMap to skip the 'password' field
        PropertyMap<UserEntity, UserDTO> propertyMap = new PropertyMap<UserEntity, UserDTO>() {
            @Override
            protected void configure() {
                // Map fields as usual
                map().setId(source.getId());
                map().setEmailId(source.getEmailId());
                map().setUserName(source.getUserName());

                // Skip mapping 'password' field
                skip().setPassword(null);
        }
            
    };
    modelMapper.addMappings(propertyMap);
    return modelMapper;
    }
Ocelot answered 2/2 at 18:16 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Portsalut

© 2022 - 2024 — McMap. All rights reserved.