Jes*_*Hun 6 java jhipster mapstruct
我的所有实体都扩展了一个具有Customer createdByandString 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);
}
Run Code Online (Sandbox Code Playgroud)
然后我利用这两个映射器:
@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)".
Run Code Online (Sandbox Code Playgroud)
如果我的业务映射器不需要额外的映射,它就可以正常工作。另外,MapStruct 似乎尝试生成两个@MapperConfig带注释的类的实现,这会产生很多错误,但它可能连接到所有与它们连接的类。
我应该如何实施这种行为?关于继承和/或共享配置的文档让我感到非常困惑。
ManageableStatus这仅在extendsEntity和ManageableStatusDTOextends时有效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;
}
}
Run Code Online (Sandbox Code Playgroud)
或者..如果 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;
}
}
Run Code Online (Sandbox Code Playgroud)
或者偷懒
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句:还有@InheritInverseConfigiration
| 归档时间: |
|
| 查看次数: |
26196 次 |
| 最近记录: |