gpt4 book ai didi

java - Spring boot Mapstruct StackOverFlow错误

转载 作者:搜寻专家 更新时间:2023-11-01 02:20:32 24 4
gpt4 key购买 nike

我正在使用 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]

谁能帮我确定为什么会循环播放?

最佳答案

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 版本不提供循环映射的“开箱即用”解决方案,它需要由用户明确完成。

关于java - Spring boot Mapstruct StackOverFlow错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45652298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com