不平凡的推土机映射

发布于 2024-08-15 06:25:48 字数 981 浏览 9 评论 0原文

我正在努力让 Dozer 屈服于我的意愿,做一些我觉得应该很简单的事情。我有两个相似的模型,我希望在它们之间进行映射,但是一个模型比另一个模型具有“更深”的层次结构,这在处理集合时给我带来了问题。考虑以下类:

源类:

class Foo {
    String id;
    NameGroup nameGroup; 
    // Setters/Getters
}

class NameGroup {
    private List<Name> names;
    // Setters/Getters
}

class Name {
    private String nameValue;
    // Setters/Getters
}

目标类:

class Bar {
    private String barId;
    private BarNames barNames;
    // Setters/Getters
}

class BarNames {
    private List<String> names;
    // Setters/Getters
}

现在我想要以下单向映射:

Foo.id -> Bar.barId // Simple enough

但我随后需要:

Foo.nameGroup.names.nameValue -> Bar.barNames.names

因此 Foo.nameGroup.namesName 实例code> 应该会导致将 String 添加到 BarNames.names 列表中。这可能吗?

I'm struggling to get Dozer to bend to my will for something that I feel should be quite simple. I have two similar models that I wish to map between, however one has a 'deeper' hierarchy than the other and this is causing me problems when dealing with collections. Consider the following classes:

Source classes:

class Foo {
    String id;
    NameGroup nameGroup; 
    // Setters/Getters
}

class NameGroup {
    private List<Name> names;
    // Setters/Getters
}

class Name {
    private String nameValue;
    // Setters/Getters
}

Destination classes:

class Bar {
    private String barId;
    private BarNames barNames;
    // Setters/Getters
}

class BarNames {
    private List<String> names;
    // Setters/Getters
}

Now I'd like the following one-way mappings:

Foo.id -> Bar.barId // Simple enough

But I then need:

Foo.nameGroup.names.nameValue -> Bar.barNames.names

So each Name instance in Foo.nameGroup.names should result in a String being added to the BarNames.names list. Is this possible?

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

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

发布评论

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

评论(1

辞旧 2024-08-22 06:25:48

只要您的“Name”类包含 String 构造函数,就可以使用 Dozer 轻松完成此操作。

来自 Dozer 文档的引用 (http://dozer.sourceforge.net/documentation/simpleproperty.html< /a>):

执行数据类型转换
通过推土机映射自动进行
引擎。目前,Dozer 支持
以下类型的转换:(这些
都是双向的)

...

字符串到
复杂类型 如果复杂类型
包含一个字符串构造函数

...

我已经用上面的类测试了它(我遇到了同样的问题)并且它工作得很好。这是我使用的映射:

<mapping>
  <class-a>com.test.bar.Bar</class-a>
  <class-b>com.test.foo.Foo</class-b>
  <field>
    <a>barId</a>
    <b>id</b>
  </field>
  <field>
    <a>barNames.names</a>
    <b>nameGroup.names</b>
    <a-deep-index-hint>java.lang.String</a-deep-index-hint>
    <b-deep-index-hint>com.test.foo.Name</b-deep-index-hint>
  </field>
</mapping>

This can easily be done with Dozer as long as your "Name" class contains a String constructor.

A quote from the Dozer docs (http://dozer.sourceforge.net/documentation/simpleproperty.html):

Data type coversion is performed
automatically by the Dozer mapping
engine. Currently, Dozer supports the
following types of conversions: (these
are all bi-directional)

...

String to
Complex Type if the Complex Type
contains a String constructor

...

I have tested this with your classes as above (I was stuck with the same problem) and it works perfectly. Here is the mapping I used:

<mapping>
  <class-a>com.test.bar.Bar</class-a>
  <class-b>com.test.foo.Foo</class-b>
  <field>
    <a>barId</a>
    <b>id</b>
  </field>
  <field>
    <a>barNames.names</a>
    <b>nameGroup.names</b>
    <a-deep-index-hint>java.lang.String</a-deep-index-hint>
    <b-deep-index-hint>com.test.foo.Name</b-deep-index-hint>
  </field>
</mapping>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文