Spring启动Mapstruct StackOverFlow错误
我正在使用mapstruct来映射我的实体和dto类……我的mapper类上有一个循环问题…
我没有想法做什么……这是我的mapper类
@Mapper(componentModel = "spring", uses = {BrandMapper.class}) public interface VehicleTypeMapper { VehicleTypeDTO vehicleTypetoVehicleTypeDTO(VehicleType vehicleType); Iterable vehicleTypetoVehicleTypeDTO(Iterable vehicleTypes); VehicleType vehicleTypeDTOtoVehicleType(VehicleTypeDTO vehicleTypeDTO); } @Mapper(componentModel = "spring", uses = { VehicleTypeMapper.class, ModelMapper.class }) public interface BrandMapper { BrandDTO brandtoBrandDTO(Brand brand); Iterable brandtoBrandDTO(Iterable 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 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 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]
有人可以帮助我找出它循环的原因吗?
您在VehicleType
和Brand
之间存在循环依赖关系。 您有3种可能来解决周期:
-
一个映射器将始终忽略循环字段。 我看到你在
VehicleType
的Brand
列表中有@JsonIgnore
。 您可以通过Mapping#ignore
器中的Mapping#ignore
忽略它们。 -
您将使用显式映射来忽略您不需要的内容,并使用限定符来选择适当的方法。 有关限定符的更多信息,请参阅文档
-
使用最新版本的
1.2.0
(在回答1.2.0.RC1
使用新的@Context
参数。查看mapstruct示例存储库中的循环映射。它解决了循环映射问题。不必使用Object
,您也可以使用您的特定类型。
注意 : 1.2.0
版本不提供“开箱即用”解决循环映射,它需要由用户明确地完成。