【问题标题】:MapStruct shared config with genericsMapStruct 与泛型共享配置
【发布时间】:2015-12-12 06:20:16
【问题描述】:

我在实体上有多个标记接口和 MappedSuperclass-es,我想创建一个共享配置,以忽略一些实体特定的目标。

示例接口和类:

public interface LongPrimaryKey extends Serializable

public abstract class FeatureBaseEntity extends BaseEntity

public class MyEntity extends FeatureBaseEntity implements LongPrimaryKey

我在 MapperConfig 界面中做了如下操作:

@MapperConfig(
    unmappedTargetPolicy = ReportingPolicy.ERROR, 
    mappingInheritanceStrategy = MappingInheritanceStrategy.AUTO_INHERIT_FROM_CONFIG
)
public interface CentralConfig {

    @Mapping(target = "id", ignore = true)
    LongPrimaryKey toLongPrimaryKey(Model model);

    @Mappings({
        @Mapping(target = "createdAt", ignore = true),
        @Mapping(target = "modifiedAt", ignore = true)
    })
    BaseEntity toBaseEntity(Model model);

    @Mappings({
        @Mapping(target = "createdAt", ignore = true),
        @Mapping(target = "modifiedAt", ignore = true),
        @Mapping(target = "id", ignore = true)
    })
    <T extends BaseEntity & LongPrimaryKey> T toBaseEntityLongPrimaryKey(Model model);

}

@Mapper(config = CentralConfig.class)
public interface MyEntityMapper {

    @Mappings({
        @Mapping(...)
    })
    @InheritConfiguration(name = "toBaseEntityLongPrimaryKey")
    MyEntity toMyEntity(MyFeatureModel myFeatureModel, @MappingTarget MyEntity entity);
}

这会抛出以下内容:

[DEBUG] diagnostic MyEntityMapper.java:28: error: None of the candidates toLongPrimaryKey(), toBaseEntity() matches given name: "toBaseEntityLongPrimaryKey".
    @InheritConfiguration(name = "toBaseEntityLongPrimaryKey")
    ^
[DEBUG] diagnostic MyEntityMapper.java:29: error: More than one configuration prototype method is applicable.
Use @InheritConfiguration to select one of them explicitly: LongPrimaryKey toLongPrimaryKey(Model model), BaseEntity toBaseEntity(Model model).
    MyEntity toMyEntity(MyFeatureModel myFeatureModel, @MappingTarget MyEntity entity);

此功能尚未涵盖,还是我遗漏了什么?

【问题讨论】:

    标签: generics mapstruct


    【解决方案1】:

    是的,这是与您的 toBaseEntityLongPrimaryKey() 方法中的 &lt;T&gt; 的类型变量有关的问题。它在例如引用toBaseEntity() 方法。

    请您在our tracker 中为这个问题打开一个问题,好吗?谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多