【问题标题】:Spring boot Mapstruct StackOverFlow Error春季启动 Mapstruct StackOverFlow 错误
【发布时间】:2018-01-20 23:32:08
【问题描述】:

我正在使用 mapstruct 来映射我的实体和 dto 类...我的映射器类上的循环有问题...

我不知道该怎么做...这是我的映射器类

    @Mapper(componentModel = "spring", uses = {BrandMapper.class})
public interface VehicleTypeMapper {

    VehicleTypeDTO vehicleTypetoVehicleTypeDTO(VehicleType vehicleType);

    Iterable<VehicleTypeDTO> vehicleTypetoVehicleTypeDTO(Iterable<VehicleType> vehicleTypes);

    VehicleType vehicleTypeDTOtoVehicleType(VehicleTypeDTO vehicleTypeDTO);
}

    @Mapper(componentModel = "spring", uses = { VehicleTypeMapper.class, ModelMapper.class })
public interface BrandMapper {

    BrandDTO brandtoBrandDTO(Brand brand);

    Iterable<BrandDTO> brandtoBrandDTO(Iterable<Brand> brands);

    Brand brandDTOtoBrand(BrandDTO brandDTO);
}

我的实体类... DTO 与我的实体类具有相同的属性...

@Entity
@Table(name = "tb_brand")
public class Brand implements Serializable {

    private static final long serialVersionUID = 1506494747401320985L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "vehicle_type_id", foreignKey = @ForeignKey(name = "fk_vehicle_type"))
    private VehicleType vehicleType;

    @JsonIgnore
    @OneToMany(mappedBy = "brand", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Model> models;

    @Column(name = "description", nullable = false)
    private String description;

//GETS AND SETS
}

@Entity
@Table(name = "tb_vehicle_type")
public class VehicleType {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @JsonIgnore
    @OneToMany(mappedBy = "vehicleType", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Brand> brands;

    @Column(name = "description", nullable = false)
    private String description;

//GETS AND SETS
}

堆栈跟踪

at br.com.meuveiculocerto.business.mapper.VehicleTypeMapperImpl.brandListToBrandDTOList(VehicleTypeMapperImpl.java:81) ~[classes/:na]
at br.com.meuveiculocerto.business.mapper.VehicleTypeMapperImpl.vehicleTypetoVehicleTypeDTO(VehicleTypeMapperImpl.java:33) ~[classes/:na]
at br.com.meuveiculocerto.business.mapper.BrandMapperImpl.brandtoBrandDTO(BrandMapperImpl.java:35) ~[classes/:na]
at br.com.meuveiculocerto.business.mapper.VehicleTypeMapperImpl.brandListToBrandDTOList(VehicleTypeMapperImpl.java:81) ~[classes/:na]

有人可以帮我确定它为什么循环吗?

【问题讨论】:

  • 在我的例子中,我已经使用了 mapstruct 接口。

标签: java spring mapstruct


【解决方案1】:

VehicleTypeBrand 之间存在循环依赖关系。您有 3 种解决周期的可能性:

  1. 一个映射器将始终忽略循环字段。我看到你在VehicleTypeBrand 列表中有@JsonIgnore。您可以通过映射器中的Mapping#ignore 忽略它们。

  2. 您将拥有显式映射,忽略您不需要的内容并使用限定符来选择适当的方法。文档中有关限定符 here 的更多信息

  3. 使用最新版本的1.2.0(在回答1.2.0.RC1时使用新的@Context参数。看看mapstruct示例库中的mapping-with-cycles。它解决了循环映射问题。你不必使用Object,你也可以使用你的特定类型来代替。

注意1.2.0 版本不提供“开箱即用”的循环映射解决方案,需要用户明确完成。

【讨论】:

  • 最好使用什么以及我如何实现?可以举个例子吗?
  • 我设置了@Mapping(target = "brands", ignore = true) 仍然不适合我,可以帮忙吗?
  • 我已将我的 pom 项目更新为 1.2.0.RC1,我需要在我的存储库中传递什么上下文? @Override public Iterable findAll() { return vehicleTypeMapper.vehicleTypestoVehicleTypeDTOs(vehicleTypeRepository.findAll(), null);我需要传递的内容在哪里?
  • 我得到这个:结果类型 java.lang.Iterable 中的未知属性“brands”。您指的是“空”吗?如果我添加@mapping 属性...
  • 我为您提供了一个指向 mapstruct 示例存储库的链接,该存储库正好解决了这个问题。您需要定义自己的@Context 类。 MapStruct 中没有这样的类。我认为带有示例的存储库链接比在答案中编写示例更相关,因为存储库可以随着时间的推移而发展,并且永远是最新的
猜你喜欢
  • 2017-10-10
  • 2017-11-05
  • 1970-01-01
  • 2014-08-12
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多