I have a Student
object extending Person
object.
public abstract class Person implements IIdentifiable {
private String contactNumber;
// other properties
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
}
public class Student extends Person {
private String studentNumber;
//Other properties
public String getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
}
Student has a property studentNumber
and person has a property contactNumber
. When I map Student object to StudentDto
it get confused on given properties.
public class StudentDto{
private String studentNumber;
public String getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
}
This happens on certain occations only. I'm wondering what would be the reason
1) The destination property com.cinglevue.veip.web.dto.timetable.StudentDto.setStudentNumber() matches multiple source property hierarchies:
com.cinglevue.veip.domain.core.student.StudentProfile.getStudent()/com.cinglevue.veip.domain.core.Person.getContactNumber()
com.cinglevue.veip.domain.core.student.StudentProfile.getStudent()/com.cinglevue.veip.domain.core.Student.getStudentNumber()
I believe that ModelMapper is based on reflection and performs the mapping during runtime. Whereas MapStruct is a code generator which generates the mapping code (java classes) during compilation time. So naturally if you are worried about performance then MapStruct is the clear choice.
ModelMapper, is an object-to-object framework that converts Java Beans (Pojos) from one representation to another. It automates different object mappings with a "convention follows configuration" approach allowing at the same time advanced functionality for cases with special needs.
ModelMapper Introduction The goal of ModelMapper is to make object mapping easy by automatically determining how one object model maps to another.
1.
You can change the MatchingStrategies
, using :
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
PS: modelmapper uses MatchingStrategies.STANDARD
implicitly
But it requires that property name tokens on the source and destination side match each other precisely.
2. Tell to the ModelMapper to ignore the mapping when it finds multiple source property hierarchies:
modelMapper.getConfiguration().setAmbiguityIgnored(true);
it's so long time since you asked. Because of no answer here, I show you my solution that works well for me.
That issue is caused by destination properties name let ModelMapper be confused. So, to resolve that, we need to do 2 steps. 1. Say ModelMapper ignore something that may be confusing. 2. Specify indicate mapping for confused properties.
Detail code is here:
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setAmbiguityIgnored(true);
modelMapper.createTypeMap(Student.class, StudentDto.class)
.addMapping(Student::getStudentNumber, StudentDto::setStudentNumber);
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