简体   繁体   English

Spring 启动 Mapstruct StackOverFlow 错误

[英]Spring boot Mapstruct StackOverFlow Error

I'm using mapstruct to map my entity and dto classes... I'm having problem with a loop on my mapper class...我正在使用 mapstruct 来映射我的实体和 dto 类......我的映射器类上的循环有问题......

I have no ideia what to do... This is my mapper classes我不知道该怎么做...这是我的映射器类

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

My entity classes... DTO is the same attributes as my entity classes...我的实体类... 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
}

THE STACK TRACE堆栈跟踪

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]

Can someone help me to identify why it's looping?有人可以帮我确定为什么它在循环吗?

You have a cyclic dependency between VehicleType and Brand .您在VehicleTypeBrand之间存在循环依赖关系。 You have 3 possibilities to resolve the cycles:您有 3 种可能性来解决循环:

  1. One mapper will always ignore the cyclic field.一个映射器将始终忽略循环字段。 I see that you have @JsonIgnore on the list of Brand in the VehicleType .我看到您在VehicleTypeBrand列表中有@JsonIgnore You could ignore them via Mapping#ignore in your mapper.您可以通过Mapping#ignore器中的Mapping#ignore忽略它们。

  2. You will have explicit mappings that ignore what you don't need and use qualifiers to choose the appropriate methods.您将拥有忽略不需要的显式映射并使用限定符来选择适当的方法。 More info about qualifiers here in the documentation约预选赛更多信息此处的文档中

  3. Use the latest release of 1.2.0 (at the time of answering 1.2.0.RC1 and use the new @Context parameter. Have a look at the mapping-with-cycles from the mapstruct examples repository. It solves cyclic mapping problems. You don't have to use Object , you can also use your specific types instead.使用1.2.0的最新版本(在回答1.2.0.RC1并使用新的@Context参数。看看 mapstruct 示例存储库中的mapping-with-cycles 。它解决了循环映射问题。你不必使用Object ,您也可以使用您的特定类型。

NOTE : The 1.2.0 release does not offer "out of the box" solving of cyclic mapping, it needs to be done by the users explicitly.注意1.2.0版本不提供循环映射的“开箱即用”解决方案,它需要由用户明确完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM