gpt4 book ai didi

java - 通过 ModelMapper 映射时将 LocalDateTime.now() 的值设置为两个字段

转载 作者:行者123 更新时间:2023-12-01 18:36:07 24 4
gpt4 key购买 nike

我必须通过 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。显然,当我尝试获取关键字段的值时,字段仍未映射。

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com