简体   繁体   English

未生成 MapStruct 一对多关系

[英]MapStruct One to Many Relationship is not generated

I want to have a simple parent children relationship but somehow it does not work and I don't get what is missing.我想有一个简单的父子关系,但不知何故它不起作用,我没有得到缺少的东西。

Parent Mapper Interface (adding uses = {LayerMapper.class} does not change anything):父映射器接口(添加uses = {LayerMapper.class}不会改变任何东西):

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
@DecoratedWith(MlpConfigMapperDecorator.class)
public interface MlpConfigMapper {

    @Mapping(target = "epochNumber", source = "epochs")
    @Mapping(target = "activationFunction", ignore = true)
    MlpConfig toEntity(CustomMlpConfigRequest mlpConfigDto);
}

Parent decorator according to this answer ( https://stackoverflow.com/a/60217018/10565504 ):根据此答案的父装饰器( https://stackoverflow.com/a/60217018/10565504 ):

public abstract class MlpConfigMapperDecorator implements MlpConfigMapper {

    @Autowired
    @Qualifier("delegate")
    private MlpConfigMapper delegate;

    @Autowired
    private ActivationFunctionService activationFunctionService;

    @Override
    public MlpConfig toEntity(CustomMlpConfigRequest mlpConfigDto) {

        MlpConfig mlpConfig = delegate.toEntity(mlpConfigDto);

        mlpConfig.setActivationFunction(activationFunctionService.findByType(mlpConfigDto.getActivationFunction()));

        return mlpConfig;
    }
}

The Parent DTO:父 DTO:

public class CustomMlpConfigRequest {

    private String name;

    private String description;

    private int batchSize;

    private int epochs;

    private List<LayerDto> layers;

    private String activationFunction;

}

The Child DTO:儿童 DTO:

public class LayerDto {

    public String type;
    public int orderNumber;
    public int neuronsNumber;

}

Parent Entity:父实体:

public class MlpConfig {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;

    private String description;

    private int batchSize;

    private int epochNumber;

    @JoinColumn(nullable = false, name = "activationFunction_id")
    @ManyToOne(fetch = FetchType.LAZY)
    private ActivationFunction activationFunction;

    @JsonManagedReference
    @Column(nullable = false)
    @OneToMany(mappedBy = "mlpConfig", cascade = CascadeType.ALL)
    private List<Layer> layers;

    @ManyToOne(fetch = FetchType.LAZY)
    private User user;

    private Date lastUpdated;
}

Child Entity:子实体:

public class Layer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private ELayer type;

    private int neuronsNumber;

    private int orderNumber;

    @EqualsAndHashCode.Exclude
    @ToString.Exclude
    @ManyToOne(fetch = FetchType.LAZY, optional=false)
    @JoinColumn(name = "mlpConfig_id", nullable=false)
    @JsonBackReference
    private MlpConfig mlpConfig;
}

Generated Child Entity Mapper Method (setChildren or setMlpConfig() in my case is missing):生成的子实体映射器方法(在我的情况下缺少 setChildren 或setMlpConfig() ):

@Override
public LayerDto layerToDto(Layer layer) {
    if ( layer == null ) {
        return null;
    }

    LayerDto layerDto = new LayerDto();

    if ( layer.getType() != null ) {
        layerDto.setType( layer.getType().name() );
    }
    layerDto.setOrderNumber( layer.getOrderNumber() );
    layerDto.setNeuronsNumber( layer.getNeuronsNumber() );

    return layerDto;
}

How do I get the mapper to set the parent in the child?如何让映射器在孩子中设置父母?

do you have Layer toEntity(LayerDto layerDto);你有Layer toEntity(LayerDto layerDto);

Also, on setter of layers on entity class, you should say, layers.forEach (layer -> layer.setmlpConfig(this));此外,在实体 class 上的层设置器上,您应该说, layers.forEach (layer -> layer.setmlpConfig(this));

Did you do that setting job?你做了那个设置工作吗? If you don't, layers of mplConfig entity can always be null when you try to get it.如果你不这样做,当你尝试获取它时,mplConfig 实体的层总是可以是 null。

In the end I fixed it myself.最后我自己修好了。 I don't know if it is best practice and probably the mapper should do it without manual help but at least it works:我不知道这是否是最佳实践,并且映射器可能应该在没有手动帮助的情况下进行,但至少它可以工作:

public abstract class MlpConfigMapperDecorator implements MlpConfigMapper {

    @Autowired
    @Qualifier("delegate")
    private MlpConfigMapper delegate;

    @Autowired
    private ActivationFunctionService activationFunctionService;

    @Override
    public MlpConfig mlpConfigToEntity(CustomMlpConfigRequest mlpConfigDto) {

        MlpConfig mlpConfig = delegate.mlpConfigToEntity(mlpConfigDto);

        mlpConfig.setActivationFunction(activationFunctionService.findByType(mlpConfigDto.getActivationFunction()));
        
        //this is the difference. I set the config for the layer manually
        mlpConfig.getLayers().forEach(e -> e.setMlpConfig(mlpConfig));

        return mlpConfig;
    }
}

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

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