Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModelMapper get confuse on different properties

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()
like image 504
Susitha Ravinda Senarath Avatar asked Mar 25 '16 07:03

Susitha Ravinda Senarath


People also ask

Does ModelMapper use reflection?

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.

What is the use of ModelMapper in java?

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.

What is ModelMapper in spring boot?

ModelMapper Introduction The goal of ModelMapper is to make object mapping easy by automatically determining how one object model maps to another.


2 Answers

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);
like image 117
Felipe Pereira Avatar answered Oct 22 '22 11:10

Felipe Pereira


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);
like image 4
code4f Avatar answered Oct 22 '22 12:10

code4f