@SubclassMapping顺序

发布于 2025-01-10 18:45:28 字数 792 浏览 4 评论 0原文

第一次使用 MapStruct (1.5.0.Beta2)

假设我有以下类层次结构:C 扩展 B 扩展 ACdto 扩展 Bdto 扩展 Adto。以及以下映射器:

@Mapper(componentModel = "spring", subclassExhaustiveStrategy = RUNTIME_EXCEPTION)
public interface MyMapper{
    @SubclassMapping(source = B.class, target = Bdto.class)
    @SubclassMapping(source = C.class, target = Cdto.class)
    Adto map(A source);
}

当我映射 C 对象列表时,我实际上得到了 Bdtos 列表。但是,如果我将顺序更改为:

@Mapper(componentModel = "spring", subclassExhaustiveStrategy = RUNTIME_EXCEPTION)
public interface MyMapper{
    @SubclassMapping(source = C.class, target = Cdto.class)
    @SubclassMapping(source = B.class, target = Bdto.class)
    Adto map(A source);
}

我会按预期获得 Cdtos 列表。这是设计使然吗?有什么办法可以让它减少对注释顺序的依赖吗?

First time using MapStruct (1.5.0.Beta2)

Say I have the following class hierarchy: C extends B extends A and Cdto extends Bdto extends Adto. And the following mapper:

@Mapper(componentModel = "spring", subclassExhaustiveStrategy = RUNTIME_EXCEPTION)
public interface MyMapper{
    @SubclassMapping(source = B.class, target = Bdto.class)
    @SubclassMapping(source = C.class, target = Cdto.class)
    Adto map(A source);
}

When I map a list of C objects I actually get a list of Bdtos. If however I change the ordering to:

@Mapper(componentModel = "spring", subclassExhaustiveStrategy = RUNTIME_EXCEPTION)
public interface MyMapper{
    @SubclassMapping(source = C.class, target = Cdto.class)
    @SubclassMapping(source = B.class, target = Bdto.class)
    Adto map(A source);
}

I get a list of Cdtos as expected. Is this by design? Is there any way to make it less dependent on annotation order?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半﹌身腐败 2025-01-17 18:45:28

这是设计使然。 这样做的原因是为了让用户控制映射的顺序。 @Mapping 注释也使用相同的行为。

您的第一个示例还应该收到编译器警告,尽管它目前可能引用错误的类型(目标而不是源)。这应该在下一版本中修复。

This is by design. The reason for this is to let the user control the order for the mappings. The same behavior is used for @Mapping annotations.

Your first example should also get a compiler warning, although it might refer to the wrong type (target instead of source) at the moment. This should be fixed in the next release.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文