MapStruct继承,不止一个配置原型是application
MapStruct inheritance, more than one configuration prototype is application
我所有的实体都扩展了一个具有 Customer createdBy
和 String createdById
字段的基本实体,还有一些功能相同。我想在数据传输时排除完整的 Customer
对象。
其他一些实体扩展了另一个实体,该实体本身扩展了基本实体,因此有以下映射器:
@MapperConfig(componentModel = "spring", mappingInheritanceStrategy = MappingInheritanceStrategy.AUTO_INHERIT_FROM_CONFIG)
public interface AuditingEntityMapper<Dto, Entity> {
@Mapping(target = "createdBy", source = "createdById")
@Mapping(target = "lastModifiedBy", source = "lastModifiedById")
Dto toDto(Entity entity);
@Mapping(target = "createdBy", ignore = true)
@Mapping(target = "lastModifiedBy", ignore = true)
Entity toEntity(Dto dto);
}
@MapperConfig(componentModel = "spring", mappingInheritanceStrategy = MappingInheritanceStrategy.AUTO_INHERIT_FROM_CONFIG)
public interface ManageableEntityMapper<ManageableDtoType extends ManageableDTO, ManageableType extends Manageable> {
ManageableDtoType toDto(ManageableType manageable);
@Mapping(target = "project", ignore = true)
ManageableType toEntity(ManageableDtoType dto);
}
然后我利用这两个映射器:
@Mapper(config = AuditingEntityMapper.class)
public interface ManageableStatusMapper {
@Mapping(target = "project", ignore = true)
@Mapping(target = "organization", ignore = true)
ManageableStatus toEntity(ManageableStatusDTO manageableStatusDTO);
default ManageableStatus fromId(String id) {
if (id == null) {
return null;
}
ManageableStatus manageableStatus = new ManageableStatus();
manageableStatus.setId(id);
return manageableStatus;
}
}
// Returns this error and therefore others about mapping problems :
// More than one configuration prototype method is applicable. Use @InheritConfiguration to select one of them explicitly: java.lang.Object toDto(java.lang.Object entity), java.lang.Object toEntity(java.lang.Object dto).
@Mapper(config = ManageableEntityMapper.class)
public interface TaskMapper {
@Mapping(target = "timeEntries", ignore = true)
@Mapping(target = "assignments", ignore = true)
Task toEntity(TaskDTO taskDTO);
default Task fromId(String id) {
if (id == null) {
return null;
}
Task task = new Task();
task.setId(id);
return task;
}
}
// Returns this error and one more of the same type :
// Can't map property "java.lang.String createdBy" to "com.acme.domain.Customer createdBy". Consider to declare/implement a mapping method: "com.acme.domain.Customer map(java.lang.String value)".
如果我的业务映射器不需要额外的映射,它就可以正常工作。
此外,MapStruct 似乎试图生成两个 @MapperConfig
注释 类 的实现,这会产生很多错误,但它可能连接到所有 类 连接到它们的实现。
我应该如何实现这种行为?关于继承and/or共享配置的doc让我很困惑。
这仅在 ManageableStatus
扩展 Entity
和 ManageableStatusDTO
扩展 Dto
时有效。
错误消息 More than one configuration prototype method is applicable. Use @InheritConfiguration
(它在您的示例中的什么位置)意味着您必须明确说明并可能指明方法的名称,因为更多的方法符合条件。
所以:
@Mapper(config = AuditingEntityMapper.class)
public interface ManageableStatusMapper {
@InheritConfiguration // use this one to inherit stuf from the config
@Mapping(target = "project", ignore = true)
@Mapping(target = "organization", ignore = true)
ManageableStatus toEntity(ManageableStatusDTO manageableStatusDTO);
default ManageableStatus fromId(String id) {
if (id == null) {
return null;
}
ManageableStatus manageableStatus = new ManageableStatus();
manageableStatus.setId(id);
return manageableStatus;
}
}
或者..如果 MapStruct 有冲突
@Mapper(config = AuditingEntityMapper.class)
public interface ManageableStatusMapper {
@InheritConfiguration( name= "toDto" ) // really point to AuditingEntityMapper#toDto
@Mapping(target = "project", ignore = true)
@Mapping(target = "organization", ignore = true)
ManageableStatus toEntity(ManageableStatusDTO manageableStatusDTO);
default ManageableStatus fromId(String id) {
if (id == null) {
return null;
}
ManageableStatus manageableStatus = new ManageableStatus();
manageableStatus.setId(id);
return manageableStatus;
}
}
或者偷懒
@Mapper(config = AuditingEntityMapper.class, mappingInheritanceStrategy = MappingInheritanceStrategy.AUTO_INHERIT_ALL_FROM_CONFIG )
public interface ManageableStatusMapper {
// no explicit @InheritConfiguration required, because of AUTO_INHERIT_ALL_FROM_CONFIG
@Mapping(target = "project", ignore = true)
@Mapping(target = "organization", ignore = true)
ManageableStatus toEntity(ManageableStatusDTO manageableStatusDTO);
default ManageableStatus fromId(String id) {
if (id == null) {
return null;
}
ManageableStatus manageableStatus = new ManageableStatus();
manageableStatus.setId(id);
return manageableStatus;
}
}
顺便说一句:还有 @InheritInverseConfigiration
我所有的实体都扩展了一个具有 Customer createdBy
和 String createdById
字段的基本实体,还有一些功能相同。我想在数据传输时排除完整的 Customer
对象。
其他一些实体扩展了另一个实体,该实体本身扩展了基本实体,因此有以下映射器:
@MapperConfig(componentModel = "spring", mappingInheritanceStrategy = MappingInheritanceStrategy.AUTO_INHERIT_FROM_CONFIG)
public interface AuditingEntityMapper<Dto, Entity> {
@Mapping(target = "createdBy", source = "createdById")
@Mapping(target = "lastModifiedBy", source = "lastModifiedById")
Dto toDto(Entity entity);
@Mapping(target = "createdBy", ignore = true)
@Mapping(target = "lastModifiedBy", ignore = true)
Entity toEntity(Dto dto);
}
@MapperConfig(componentModel = "spring", mappingInheritanceStrategy = MappingInheritanceStrategy.AUTO_INHERIT_FROM_CONFIG)
public interface ManageableEntityMapper<ManageableDtoType extends ManageableDTO, ManageableType extends Manageable> {
ManageableDtoType toDto(ManageableType manageable);
@Mapping(target = "project", ignore = true)
ManageableType toEntity(ManageableDtoType dto);
}
然后我利用这两个映射器:
@Mapper(config = AuditingEntityMapper.class)
public interface ManageableStatusMapper {
@Mapping(target = "project", ignore = true)
@Mapping(target = "organization", ignore = true)
ManageableStatus toEntity(ManageableStatusDTO manageableStatusDTO);
default ManageableStatus fromId(String id) {
if (id == null) {
return null;
}
ManageableStatus manageableStatus = new ManageableStatus();
manageableStatus.setId(id);
return manageableStatus;
}
}
// Returns this error and therefore others about mapping problems :
// More than one configuration prototype method is applicable. Use @InheritConfiguration to select one of them explicitly: java.lang.Object toDto(java.lang.Object entity), java.lang.Object toEntity(java.lang.Object dto).
@Mapper(config = ManageableEntityMapper.class)
public interface TaskMapper {
@Mapping(target = "timeEntries", ignore = true)
@Mapping(target = "assignments", ignore = true)
Task toEntity(TaskDTO taskDTO);
default Task fromId(String id) {
if (id == null) {
return null;
}
Task task = new Task();
task.setId(id);
return task;
}
}
// Returns this error and one more of the same type :
// Can't map property "java.lang.String createdBy" to "com.acme.domain.Customer createdBy". Consider to declare/implement a mapping method: "com.acme.domain.Customer map(java.lang.String value)".
如果我的业务映射器不需要额外的映射,它就可以正常工作。
此外,MapStruct 似乎试图生成两个 @MapperConfig
注释 类 的实现,这会产生很多错误,但它可能连接到所有 类 连接到它们的实现。
我应该如何实现这种行为?关于继承and/or共享配置的doc让我很困惑。
这仅在 ManageableStatus
扩展 Entity
和 ManageableStatusDTO
扩展 Dto
时有效。
错误消息 More than one configuration prototype method is applicable. Use @InheritConfiguration
(它在您的示例中的什么位置)意味着您必须明确说明并可能指明方法的名称,因为更多的方法符合条件。
所以:
@Mapper(config = AuditingEntityMapper.class)
public interface ManageableStatusMapper {
@InheritConfiguration // use this one to inherit stuf from the config
@Mapping(target = "project", ignore = true)
@Mapping(target = "organization", ignore = true)
ManageableStatus toEntity(ManageableStatusDTO manageableStatusDTO);
default ManageableStatus fromId(String id) {
if (id == null) {
return null;
}
ManageableStatus manageableStatus = new ManageableStatus();
manageableStatus.setId(id);
return manageableStatus;
}
}
或者..如果 MapStruct 有冲突
@Mapper(config = AuditingEntityMapper.class)
public interface ManageableStatusMapper {
@InheritConfiguration( name= "toDto" ) // really point to AuditingEntityMapper#toDto
@Mapping(target = "project", ignore = true)
@Mapping(target = "organization", ignore = true)
ManageableStatus toEntity(ManageableStatusDTO manageableStatusDTO);
default ManageableStatus fromId(String id) {
if (id == null) {
return null;
}
ManageableStatus manageableStatus = new ManageableStatus();
manageableStatus.setId(id);
return manageableStatus;
}
}
或者偷懒
@Mapper(config = AuditingEntityMapper.class, mappingInheritanceStrategy = MappingInheritanceStrategy.AUTO_INHERIT_ALL_FROM_CONFIG )
public interface ManageableStatusMapper {
// no explicit @InheritConfiguration required, because of AUTO_INHERIT_ALL_FROM_CONFIG
@Mapping(target = "project", ignore = true)
@Mapping(target = "organization", ignore = true)
ManageableStatus toEntity(ManageableStatusDTO manageableStatusDTO);
default ManageableStatus fromId(String id) {
if (id == null) {
return null;
}
ManageableStatus manageableStatus = new ManageableStatus();
manageableStatus.setId(id);
return manageableStatus;
}
}
顺便说一句:还有 @InheritInverseConfigiration