【问题标题】:Set the value of LocalDateTime.now() to two fields while mapping by ModelMapper通过 ModelMapper 映射时将 LocalDateTime.now() 的值设置为两个字段
【发布时间】:2020-02-03 22:30:09
【问题描述】:

我必须通过 ModelMapper 将我从 UI 获取的 DTO 映射到我的实体。

在使用 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。显然,在我尝试获取 Key 字段的值时,字段仍未映射。

那么,有没有人知道如何处理这个时刻并使 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;
}

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

【问题讨论】:

  • 能否请您也添加您的 POJO(s) 结构? (Operation, OperationForm, OperationKey...)

标签: java spring modelmapper


【解决方案1】:

似乎我找到了适合我的解决方案。

我现在对 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 并将我的方法推入其中。现在可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-03
    • 2019-06-12
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2020-04-06
    相关资源
    最近更新 更多