java - MapStruct继承,不止一个配置原型(prototype)是application

标签 java jhipster mapstruct

我的所有实体都扩展了一个基本实体,该实体具有 Customer createdByStringcreatedById 字段,以及其他一些工作原理相同的实体。我想在传输数据时排除完整的 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 注释类的实现,这会生成很多错误,但它可能连接到所有连接到它们的类。

我应该如何实现这种行为? doc关于继承和/或共享配置让我很困惑。

最佳答案

仅当 ManageableStatus 扩展 EntityManageableStatusDTO 扩展 Dto 时才有效。

错误消息不止一种配置原型(prototype)方法适用。使用 @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

关于java - MapStruct继承,不止一个配置原型(prototype)是application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57860451/

相关文章:

java - 在 ReST 响应中返回 JSP

Mapstruct - 将 customModel 映射到它的字符串表示形式

java - 创建通用的 ObjectMapper 方法以跨多个 POJO 类进行反序列化

java - Swift 中的 Java Object 等价物是什么?

java - 我怎样才能让 Tomcat 解释一个 jar 库?

java - Jhipster 中的生产数据库创建是如何进行的?

java - 如何防止重新生成 jHipster 实体时删除自定义存储库方法

jboss - 如何在 JBOSS EAP 6.4 上运行 Jhipster 5.4.0 war

java - 映射结构 : abstract target class and concrete type based on discriminator field

java - 使用生成类通过 2 个映射器通过 MapStruct 传输对象