简体   繁体   English

Mapstruct双向映射抛出内存不足错误

[英]Mapstruct bidirectional mapping throwing out of memory error

//Entities and DTOs

public class JourneyType implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "journey_type_id")
    private Long journeyTypeId;

    private String type;

    // bi-directional many-to-one association to JourneyRent
    @OneToMany(mappedBy = "journeyType")
    private Set<JourneyRent> journeyRents;
}

public class JourneyTypeTO implements Serializable {

    private Long journeyTypeId;
    private String type;
    private Set<JourneyRentTO> journeyRents;
}

public class JourneyRent implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "journey_rent_id")
    private Long journeyRentId;

    @Column(name = "min_max_travel_km")
    private Double minMaxTravelKm;

    @Column(name = "rent_charges")
    private BigDecimal rentCharges;

    // bi-directional many-to-one association to JourneyType
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "journey_type_id")
    private JourneyType journeyType;
    
    // bi-directional many-to-one association to VehicleType
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "vehicle_type_id")
    private VehicleType vehicleType;
}

public class JourneyRentTO implements Serializable {

    private Long journeyRentId;
    private Double minMaxTravelKm;
    private BigDecimal rentCharges;
    private JourneyTypeTO journeyType;  
    private VehicleTypeTO vehicleType;
}

public class VehicleType implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="vehicle_type_id")
    private Long vehicleTypeId;

    private String type;

    //bi-directional many-to-one association to JourneyRent
    @OneToMany(mappedBy="vehicleType")
    private Set<JourneyRent> journeyRents;
}

public class VehicleTypeTO implements Serializable {
    private Long vehicleTypeId;
    private String type;
    private Set<JourneyRentTO> journeyRents;
}

// Mapper interfaces
@Mapper(uses = {JourneyRentMapper.class}, unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface JourneyTypeMapper {
    JourneyTypeMapper INSTANCE = Mappers.getMapper(JourneyTypeMapper.class);
    
    @Mapping(target = "journeyBookings", ignore = true)
    JourneyTypeTO toDTO(JourneyType journeyType, @Context CycleAvoidingMappingContext cycleAvoidingMappingContext);

    @Mapping(target = "journeyBookings", ignore = true)
    JourneyType toEntity(JourneyTypeTO journeyType);
}

@Mapper(uses = {JourneyTypeMapper.class, VehicleTypeMapper.class}, unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface JourneyRentMapper { 
    JourneyRentMapper INSTANCE = Mappers.getMapper(JourneyRentMapper.class);

    @Mapping(target = "journeyType", ignore = true)
    JourneyRentTO toDTO(JourneyRent journeyRent, @Context CycleAvoidingMappingContext cycleAvoidingMappingContext);
    
    @Mapping(target = "journeyBookingVehicles", ignore = true)
    JourneyRent toEntity(JourneyRentTO journeyRentTo);
}

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface VehicleTypeMapper {
    VehicleTypeMapper INSTANCE = Mappers.getMapper(VehicleTypeMapper.class);

    VehicleTypeTO toDTO(VehicleType vehiType);

    VehicleType toEntity(VehicleTypeTO vehiTypeTo);
}

public class CycleAvoidingMappingContext {
    private final Map<Object, Object> knownInstances = new IdentityHashMap<>();

    @BeforeMapping
    public <T> T getMappedInstance(Object source, @TargetType Class<T> targetType) {
        return targetType.cast(knownInstances.get(source));
    }

    @BeforeMapping
    public void storeMappedInstance(Object source, @MappingTarget Object target) {
        knownInstances.put(source, target);
    }
}

There is a bi-directional relationship between JourneyType and JourneyRent. JourneyType 和 JourneyRent 之间存在双向关系。 Now JPQL query returns JourneyType Object which has a reference of JourneyRent object and in turn JourneyRent object has a reference of JourneyType Object.现在 JPQL 查询返回 JourneyType 对象,该对象具有 JourneyRent 对象的引用,而 JourneyRent 对象又具有 JourneyType 对象的引用。

Usage :用法 :

List<JourneyTypeTO> journeyTypeTos = new ArrayList<>();
    for (JourneyType journeyType : journeyTypes) {
        journeyTypeTos.add(JourneyTypeMapper.INSTANCE.toDTO(journeyType, new CycleAvoidingMappingContext()));
}

When I try to map Entity to TO, I'm getting out of memory error.当我尝试将实体映射到 TO 时,出现内存不足错误。 How can I map the objects for this scenario ?如何映射此场景的对象?

I see that you are using the example of the CycleAvoidingMappingContext .我看到您正在使用CycleAvoidingMappingContext的示例。 The reason why it isn't working to you is because you are not passing it in your VehicleMapper .它对您不起作用的原因是因为您没有在VehicleMapper传递它。 MapStruct will use the VehicleMapper to map your vehicles and there is no context that would store the cycles. MapStruct 将使用VehicleMapper来映射您的车辆,并且没有可以存储循环的上下文。

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

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