我必须将我从UI获取的DTO映射到ModelMapper。

在使用ModelMapper之前,我有以下方法:

private Operation map(OperationForm src) {
    Operation operation = new Operation();
    LocalDateTime NOW = LocalDateTime.now();
    operation.setOperationKey(OperationKey.of(src.getLegalEntityId(),
                             src.getEmployeeId(),
                             NOW));  // here
    operation.setIndividualId(src.getIndividualId());
    operation.setKey(src.getLegalEntityId() + "_"
                     + src.getEmployeeId() + "_"
                     + NOW);         // and here

    return operation;
}


在这种情况下,我必须通过LocalDateTime.now()生成新的LocalDateTime,并在两个不同的字段中使用它。

因此,我尝试像这样配置ModelMapper:

public OperationFormMapper(ModelMapper modelMapper) {
    Converter<OperationForm, OperationKey> operationKeyConverter = (context) -> OperationKey.of(context.getSource().getLegalEntityId(),
                context.getSource().getEmployeeId(),
                LocalDateTime.now()); // generate new LocalDateTime to
                                      // destination instance

    Converter<Operation, String> keyConverter = (context) -> {
        Operation src = context.getSource();
        return src.getOperationKey().getLegalEntityId() + "_"
               + src.getOperationKey().getEmployeeId() + "_"
               + src.getOperationKey().getOperationDate();
    }; // converter for my purpose
       // I am trying to get value not from source,
       // but from destination instance, hoping that
       // all needed fields are already mapped

    this.modelMapper = modelMapper;

    this.modelMapper.addMappings(new PropertyMap<OperationForm, Operation>() {

        @Override
        protected void configure() {
            using(operationKeyConverter).map(source, destination.getOperationKey());
            map().setIndividualId(source.getIndividualId());
            map().setOperationStatus(Operation.OperationStatus.CREATED);

            using(keyConverter).map(destination, destination.getKey());
            // trying to get all needed data from already mapped fields
        }
    });
}


它没有起作用也就不足为奇了。我收到了NullPointerException。显然,在我尝试获取键字段的值时,字段仍未映射。

那么,有人知道如何处理这一时刻并使ModelMapper也适用于我的情况吗?或者,也许至少有一些方法可以完全自定义映射过程,并创建将由ModelMapper用于映射的整个方法(或使其使用上面的现有方法)?

更新:

我的实体:

public class Operation {
    private OperationKey operationKey;
    private Long individualId;
    private String operationStatus;
    private String operationResult;
    private String guardCardSeries;
    private String guardCardNumber;
    private LocalDateTime guardCardStartDate;
    private LocalDateTime guardCardEndDate;
    private String resultMessage;
    private String key;
}


我的UI DTO:

public class OperationForm implements Serializable {
    private Long legalEntityId;
    private Long employeeId;
    private Long individualId;
}


我的Cassandra DB复合键:

public class OperationKey {
    private final Long legalEntityId;
    private final Long employeeId;
    private final LocalDateTime operationDate;
}



我只是通过删除持久性注释来简化了它们。

最佳答案

好像我找到了解决方案。

现在我对ModelMapper的配置。

public OperationFormMapper(ModelMapper modelMapper) {
        this.modelMapper = modelMapper;

        Converter<OperationForm, Operation> converter = context -> {
            OperationForm src = context.getSource();
            Operation dest = new Operation();

            final LocalDateTime NOW = LocalDateTime.now();
            dest.setOperationKey(OperationKey.of(src.getLegalEntityId(), src.getEmployeeId(), NOW));
            dest.setIndividualId(src.getIndividualId());
            dest.setKey(src.getLegalEntityId() + "_" + src.getEmployeeId() + "_" + NOW);

            return dest;
        };

        this.modelMapper.addConverter(converter);
    }



我创建了Converter并将其方法推入其中。现在可以了。

关于java - 通过ModelMapper映射时,将LocalDateTime.now()的值设置为两个字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60040328/

10-10 18:16