9

我有一个Student对象扩展Person对象。

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;
    }
}

学生有财产studentNumber,人有财产contactNumber。当我将 Student 对象映射到StudentDto它时,会对给定的属性感到困惑。

public class StudentDto{
    private String studentNumber;
    public String getStudentNumber() {
        return studentNumber;
    }

    public void setStudentNumber(String studentNumber) {
        this.studentNumber = studentNumber;
    }
}

这只发生在某些场合。我想知道是什么原因

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()
4

2 回答 2

4

1. 你可以改变MatchingStrategies, 使用 :

modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

PS:modelmapperMatchingStrategies.STANDARD隐式使用

但它要求源端和目标端的属性名称标记相互精确匹配。

2. 告诉 ModelMapper 在发现多个源属性层次结构时忽略映射:

modelMapper.getConfiguration().setAmbiguityIgnored(true);
于 2020-05-09T05:07:07.697 回答
1

好久没问了。由于这里没有答案,我向您展示了我的解决方案,它对我很有效。

该问题是由目标属性名称引起的,让 ModelMapper 感到困惑。所以,要解决这个问题,我们需要做两个步骤。1. 假设 ModelMapper 忽略了一些可能令人困惑的东西。2. 指定混淆属性的指示映射。

详细代码在这里:

ModelMapper modelMapper = new ModelMapper();

modelMapper.getConfiguration().setAmbiguityIgnored(true);

modelMapper.createTypeMap(Student.class, StudentDto.class)
    .addMapping(Student::getStudentNumber, StudentDto::setStudentNumber);
于 2018-09-26T06:32:56.963 回答