Mapstruct避免List property in B class的循环发布问题

问题描述 投票:0回答:2

如果我有一个类ProfessorDto和一个类StudentDto,如果ProfessorDto拥有StudentDto的列表,而StudentDto拥有ProfessorDto类型的属性,如何避免出现循环问题?我没有放入域类的代码,但可以说它与Dto相同。

我是Mapstruct的新手,将域bean转换为具有Long,String之类的简单属性的Dto起作用,但在我的示例中,关系OneToMany不起作用!

@JsonApiResource(type = "professor")
@NoArgsConstructor
@Data
public class ProfessorDto {

  @JsonApiId
  private Long id;

  private String professorName;

  @JsonApiRelation(mappedBy = "professor")
  private List<StudentDto> student;

  public ProfessorDto(Long id) {
    this.id = id;
  }
}

和班级学生

@JsonApiResource(type = "student")
@NoArgsConstructor
@Data
public class StudentDto {

  @JsonApiId
  private Long id;

  private String studentName;

  @JsonApiRelation
  private ProfessorDto professor;
}

[我的教授教授]是>

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface ProfessorMapper {

  ProfessorDto domainToDto(Professor domain);

  Professor dtoToDomain(ProfessorDto dto);

  StudentDto studentToDto(Student student);

  Student studentDtoToDomain(StudentDto studentDto);

  List<StudentDto> studentToDto(List<Student> student);

  List<Student> studentDtoToDomain(List<StudentDto> studentDto);
}

如果我有班级ProfessorDto和班级StudentDto,如果ProfessorDto具有StudentDto的列表,而StudentDto具有ProfessorDto类型的属性,如何避免出现循环问题?我没有...

java list circular-dependency relation mapstruct
2个回答
0
投票

使用@Context注释查看此示例


0
投票

首先,应该确定是否确实需要将List<StudentDto>聚合在ProfessorDto中。如果可能,您可以将其排除。否则,您可以将StudentDtoProfessor设置为“扁平”。例如,您可以将字段Long professorId而不是ProfessorDto professor添加到StudentDto,或将List<Long> studentIds添加到ProfessorDto

© www.soinside.com 2019 - 2024. All rights reserved.