首页 文章

Modelmapper循环映射

提问于
浏览
1

我有各自的DTO的父母和子女课程,如下所示

class Parent {
    List<Child> children;
    // setters and getters
}

class Child {
    Parent parent;
}

class ParentDto {
    List<ChildDto> children;
    // setters and getters
}

class ChildDto {
    ParentDto parent;
    // setters and getters
}

当我尝试将Parent映射到ParentDto时,我得到了StackOverflowError .

请帮我解决这个问题 .

1 回答

  • 0

    我知道这篇文章已经过时了,但我会尝试给出一个答案,以防谷歌有人来到这里 .

    可能你有动态对象创建 .

    最近我遇到了这样的问题 . 在我的情况下,我有一个扩展Person的 class 学生 . 而且Person还有其他三种属性:父亲,母亲和责任人 . 在这种情况下,我有一个循环关系 . 当我运行我的JUnits测试时,我得到了循环调用 . 我没有得到stakOverflow因为Spring(一段时间后)关闭了数据库连接 .

    我解决了这个问题,从我的Person类中删除了动态对象创建 .

    另一种解决此问题的方法是在mapper中添加一个Condition . 您可以阅读有关条件here的更多信息 . 例如,您可以有一些条件说:"If the person attribute id is 10, then, doesn't need map it." . 这样我们就可以避免无穷大映射 . 好吧,让我们看一些代码片段:

    学生班:

    public class Student extends Person implements IStudent {
    
        private Person financialResponsible;
    
        // Getters and setters.
    
    }
    

    人员类:

    public class Person implements IPerson {
    
        private Long id;
        private String name;
        private Person father;
        private Person mother;
        private Person responsible;
        private Address address;
    
        // Getters and setters.
    
        // This is my real problem. I removed it and the mapper worked.
        public IPerson getFather() {
            if (father == null) father = new Person();
            return father; 
        }
    }
    

    StudentDTO课程:

    public class StudentDTO extends PersonDTO {
    
        private PersonDTO financialResponsible;
    
        // Getters and setters.
    }
    

    PersonDTO课程:

    public class PersonDTO {
    
        private Long id;
        private String name;
        private PersonDTO father;
        private PersonDTO mother;
        private PersonDTO responsible;
        private AddressDTO address;
    
        // Getters and setters.
    
    }
    

    以下是一个条件示例:

    ...
    import org.modelmapper.Condition;
    ...
    
        ModelMapper mapper = new ModelMapper();
    
        // Define STRICT to give high precision on mapper.
        mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
    
        /*
         * Create conditions to avoid infinity circularity.
         */
        // Condidions.
        final Condition<IStudent, StudentDTO> fatherIsTen = mappingContext -> mappingContext.getSource().getFather().getId() == 10;
        final Condition<IStudent, StudentDTO> motherIsTen = mappingContext -> mappingContext.getSource().getMother().getId() == 10;
        final Condition<IStudent, StudentDTO> resposibleIsTen = mappingContext -> mappingContext.getSource().getResponsible().getId() == 10;
    
        // Adding conditions on mapper.
        mapper.createTypeMap(IStudent.class, StudentDTO.class) //
           .addMappings(mapper -> mapper.when(fatherIsTen).skip(StudentDTO::setFather))
           .addMappings(mapper -> mapper.when(motherIsTen).skip(StudentDTO::setMother))
           .addMappings(mapper -> mapper.when(resposibleIsTen).skip(StudentDTO::setResponsible));
    

    我希望有帮助o /

相关问题