如何将 parent class DTO 映射到 parent class 实体
How do I map parent class DTO to parent class entity
我有以下 class 层次结构:
public class BaseFaultType
{
protected String faultId;
protected String faultDescription;
protected String faultDetail;
... -> setters and getters
}
public class PollingFaultType extends BaseFaultType
{
protected Integer initiatedFiles;
protected Integer failedFiles;
... -> getters and setters
}
public class StoreFaultType extends BaseFaultType
{
protected String storePath;
... -> getters and setters
}
...
有一种具有一些通用属性的 BaseFaultType(为简单起见,我省略了其中的大部分),然后我有多种类型可以使用其他属性扩展 BaseFaultType。
请注意,我无法控制这些 classes。
我的应用程序收到 objects 这些 child 类型。
然后我需要将这些 objects 映射到不同类型的实体中,即:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = EntityConstants.ERROR)
public class FaultMessage
{
private String errorId;
private String errorDescription;
private String errorDetail;
private Boolean retryable;
... -> getters and setters
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = EntityConstants.POLLING_MESSAGE)
public class PollingFaultMessage extends FaultMessage
{
private Integer failed;
private Integer initiated;
... -> getters and setters
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = EntityConstants.STORE_MESSAGE)
public class StoreFaultMessage
{
...
}
我正在使用 ModelMapper 来完成这项工作。
请注意,我使用的 ModelMapper 配置为禁用隐式映射(出于对这个问题不感兴趣的原因):
mapper.getConfiguration().setImplicitMappingEnabled(false);
mapper.addMappings(new PropertyMap<BaseFaultType, FaultMessage>()
{
@Override
protected void configure()
{
map(source.getFaultId()).setErrorId(null);
map(source.getFaultDescription()).setErrorDescription(null);
...
}
});
mapper.addMappings(new PropertyMap<PollingFaultType, PollingFaultMessage>()
{
@Override
protected void configure()
{
map(source.getInitiatedFiles()).setInitiated(null);
...
}
});
PollingFaultType faultType = getPollingFaultType();
PollingFaultMessage faultMessage = mapper.map(faultType, PollingFaultMessage.class);
不幸的是,这会产生仅将其 initiated
属性 映射到实际值的故障消息。
属性 errorId
、errorDetail
等未被映射(可能是因为它们被配置到一个完全独立的 TypeMap 中)
所以我的问题是 - 如何配置 ModelMapper 以允许我定义 TypeMaps/PropertyMaps 仅映射 child-specific 属性,例如 initiated
和 failed
并自动映射来自基本类型(例如 errorId
和 errorDetail
的 common 属性?
我想在这里实现的主要目标是我想避免在每个 child 的 TypeMap 中显式指定这些 common 属性的映射,即我想避免至:
mapper.addMappings(new PropertyMap<PollingFaultType, PollingFaultMessage>()
{
@Override
protected void configure()
{
// I want to avoid to copy these lines around for each child mapping since they will be always the same
map(source.getFaultId()).setErrorId(null);
map(source.getFaultDescription()).setErrorDescription(null);
map(source.getInitiatedFiles()).setInitiated(null);
...
}
});
尝试下一步:
豆子:
public class BaseType {
protected String base;
// getters/setters
}
public class Extended extends BaseType {
private String value ;
// getters/setters
}
dto:
public class SimpleDTO {
private String valueDTO;
private String baseDTO;
// getters/setters
}
配置:
modelMapper.typeMap(SimpleDTO.class, BaseType.class)
.addMapping(SimpleDTO::getBaseDTO, BaseType::setBase);
modelMapper.typeMap(SimpleDTO.class, Extended.class)
.addMapping(SimpleDTO::getValueDTO, Extended::setValue)
.includeBase(SimpleDTO.class, BaseType.class);
使用:
SimpleDTO dto = new SimpleDTO();
dto.setBaseDTO("base");
dto.setValueDTO("value");
Extended ex = modelMapper.map(dto, Extended.class);
ModelMapper 版本:1.1.0
我有以下 class 层次结构:
public class BaseFaultType
{
protected String faultId;
protected String faultDescription;
protected String faultDetail;
... -> setters and getters
}
public class PollingFaultType extends BaseFaultType
{
protected Integer initiatedFiles;
protected Integer failedFiles;
... -> getters and setters
}
public class StoreFaultType extends BaseFaultType
{
protected String storePath;
... -> getters and setters
}
...
有一种具有一些通用属性的 BaseFaultType(为简单起见,我省略了其中的大部分),然后我有多种类型可以使用其他属性扩展 BaseFaultType。
请注意,我无法控制这些 classes。
我的应用程序收到 objects 这些 child 类型。 然后我需要将这些 objects 映射到不同类型的实体中,即:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = EntityConstants.ERROR)
public class FaultMessage
{
private String errorId;
private String errorDescription;
private String errorDetail;
private Boolean retryable;
... -> getters and setters
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = EntityConstants.POLLING_MESSAGE)
public class PollingFaultMessage extends FaultMessage
{
private Integer failed;
private Integer initiated;
... -> getters and setters
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = EntityConstants.STORE_MESSAGE)
public class StoreFaultMessage
{
...
}
我正在使用 ModelMapper 来完成这项工作。 请注意,我使用的 ModelMapper 配置为禁用隐式映射(出于对这个问题不感兴趣的原因):
mapper.getConfiguration().setImplicitMappingEnabled(false);
mapper.addMappings(new PropertyMap<BaseFaultType, FaultMessage>()
{
@Override
protected void configure()
{
map(source.getFaultId()).setErrorId(null);
map(source.getFaultDescription()).setErrorDescription(null);
...
}
});
mapper.addMappings(new PropertyMap<PollingFaultType, PollingFaultMessage>()
{
@Override
protected void configure()
{
map(source.getInitiatedFiles()).setInitiated(null);
...
}
});
PollingFaultType faultType = getPollingFaultType();
PollingFaultMessage faultMessage = mapper.map(faultType, PollingFaultMessage.class);
不幸的是,这会产生仅将其 initiated
属性 映射到实际值的故障消息。
属性 errorId
、errorDetail
等未被映射(可能是因为它们被配置到一个完全独立的 TypeMap 中)
所以我的问题是 - 如何配置 ModelMapper 以允许我定义 TypeMaps/PropertyMaps 仅映射 child-specific 属性,例如 initiated
和 failed
并自动映射来自基本类型(例如 errorId
和 errorDetail
的 common 属性?
我想在这里实现的主要目标是我想避免在每个 child 的 TypeMap 中显式指定这些 common 属性的映射,即我想避免至:
mapper.addMappings(new PropertyMap<PollingFaultType, PollingFaultMessage>()
{
@Override
protected void configure()
{
// I want to avoid to copy these lines around for each child mapping since they will be always the same
map(source.getFaultId()).setErrorId(null);
map(source.getFaultDescription()).setErrorDescription(null);
map(source.getInitiatedFiles()).setInitiated(null);
...
}
});
尝试下一步:
豆子:
public class BaseType {
protected String base;
// getters/setters
}
public class Extended extends BaseType {
private String value ;
// getters/setters
}
dto:
public class SimpleDTO {
private String valueDTO;
private String baseDTO;
// getters/setters
}
配置:
modelMapper.typeMap(SimpleDTO.class, BaseType.class)
.addMapping(SimpleDTO::getBaseDTO, BaseType::setBase);
modelMapper.typeMap(SimpleDTO.class, Extended.class)
.addMapping(SimpleDTO::getValueDTO, Extended::setValue)
.includeBase(SimpleDTO.class, BaseType.class);
使用:
SimpleDTO dto = new SimpleDTO();
dto.setBaseDTO("base");
dto.setValueDTO("value");
Extended ex = modelMapper.map(dto, Extended.class);
ModelMapper 版本:1.1.0