当实体与另一个实体有关系时,如何使用ModelMapper
将实体转换为 DTO 。OneToMany
我有一个实体学生
@Getter
@Setter
public class Student{
private int name;
private Set<Department> department;
}
@Getter
@Setter
public class Department{
private int name;
}
@Getter
@Setter
public class StudentDto{
private int name;
private Set<DepartmentDto> departmentDto;
}
@Getter
@Setter
public class DepartmentDto{
private int name;
private int name;
}
我正在使用以下方法将实体转换为 DTO:-
private List<StudentDto> convertToDto(List<Student> Student) {
List<StudentDto> studentDtos= new LinkedList<>();
Iterator<Student> student= student.iterator();
while (student.hasNext()) {
Student student2= student.next();
studentDtos.add(modelMapper.map(student2, StudentDTo.class));
}
return studentDtos;
}
当我这样做时,我在我的控制器中调用它,在那里我可以看到 Department DTo 为空。你能告诉我我在哪里做错了吗?