How should ModelMapper configuration looks like?
compile group: 'org.modelmapper', name: 'modelmapper', version: '2.3.8'
Currently I have:
modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setMatchingStrategy(MatchingStrategies.STANDARD)
.setFieldMatchingEnabled(true)
.setFieldAccessLevel(Configuration.AccessLevel.PRIVATE);
My Entity has
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Phones> phones = new ArrayList<>();
My second entity:
@ManyToOne
@JoinColumn(name = "employee_id")
private Employee employee;
My DTO which I want to have:
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor(force = true)
public class EmployeeDTO {
@Singular
private final List<PhoneDTO> phones;
}
I am mapping this in this way:
EmployeeDTO.EmployeeDTOBuilder employeeDTOBuilder
= ObjectMapperUtils.map(employee, EmployeeDTO.employeeDTOBuilder.class);
I'm getting:
Look a bit deeper in the stack trace: the last mapping problem is often something more actionable.
Maybe the PhoneDTO
was not mappable ? If so, you need to create a converter and tell model mapper about it, like so:
public class PhoneConverter extends AbstractConverter<PhoneEntity, PhoneDto>
{
private final StrictModelMapper strictModelMapper;
@Override
public PhoneDto convert(@NonNull final PhoneEntity source)
{
PhoneDto phoneDto = new PhoneDto();
strictModelMapper.map(source, phoneDto);
return phoneDto;
}
}
and add it to the modelMapper
.
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