问题描述
我正在使用mapstruct映射我的实体和dto类...我在mapper类上存在循环问题...
我没有意识形态该怎么办...这是我的映射器课程
@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]
有人可以帮助我确定其循环原因吗?
您在VehicleType
和Brand
之间具有循环依赖关系.您可以通过以下三种方式解决周期问题:
-
一个映射器将始终忽略循环字段.我看到您在
VehicleType
的Brand
列表中有@JsonIgnore
.您可以通过映射器中的Mapping#ignore
忽略它们. -
您将拥有显式映射,这些映射将忽略不需要的内容,并使用限定符选择适当的方法.有关限定符的更多信息,请此处 p>
-
使用最新版本的
1.2.0
(在回答1.2.0.RC1
时,并使用新的@Context
参数.请查看映射与循环,它解决了循环映射问题.要使用Object
,您也可以改用您的特定类型.
注意:1.2.0
版本没有提供开箱即用"的循环映射解决方案,它需要由用户明确地完成.
I'm using mapstruct to map my entity and dto classes... I'm having problem with a loop on my mapper class...
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...
@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
. You have 3 possibilities to resolve the cycles:
One mapper will always ignore the cyclic field. I see that you have
@JsonIgnore
on the list ofBrand
in theVehicleType
. You could ignore them viaMapping#ignore
in your mapper.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
Use the latest release of
1.2.0
(at the time of answering1.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 useObject
, you can also use your specific types instead.
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.
这篇关于Spring Boot Mapstruct StackOverFlow错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!