ModelMapper get confuse on different properties
Asked Answered
R

2

10

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()
Roehm answered 25/3, 2016 at 7:19 Comment(3)
can you please post your tried code ,as well as Student & Person classTribune
Sure. I update the postRoehm
Could you post your ModelMapper configuration and map? Or simply the code case map when the error is happen.Whiffler
P
5

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);
Paoting answered 9/5, 2020 at 5:7 Comment(1)
I cannot remember why I had this problem now. Anyway someone might benefit. ThanksRoehm
K
4

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);
Kingly answered 26/9, 2018 at 6:32 Comment(2)
yes, I cannot remember why I had this problem now. Anyway someone might benefit. ThanksRoehm
How do you do that with nested objects?Mistletoe

© 2022 - 2024 — McMap. All rights reserved.