将深层属性与推土机中的中间集合映射

发布于 2024-08-20 17:06:55 字数 732 浏览 7 评论 0原文

假设我有以下类

public class Baz {
  private List<Foo> foos = new ArrayList<Foo>();
}

public class Foo {
  private String string;
}

public class Target {
  private List<String> fooStrings = new ArrayList<String>();
}

,给定一个 Baz,我可以使用任何映射将其映射到目标类并获取 Baz 中 foo 中包含的字符串列表吗?以下映射不起作用,

<mapping>
  <class-a>Baz</class-a>
  <class-b>Target</class-b>
  <field>
    <a>foos.string</a>
    <b>fooStrings</b>
  </field>
</mapping>

因为 string 不是 foos 的属性(其类型为 List)。我本以为 Dozer 会足够聪明,如果它在深度映射中遇到一个集合,并且目标也是一个集合,能够将深度属性名称分成两部分并迭代集合以获取子部分来自集合成员的深度映射。显然不是。除了提出 Dozer 的功能请求之外,还有其他解决方案吗?

Suppose I have the following classes

public class Baz {
  private List<Foo> foos = new ArrayList<Foo>();
}

public class Foo {
  private String string;
}

public class Target {
  private List<String> fooStrings = new ArrayList<String>();
}

Is there any mapping I can use to, given a Baz, map it to the target class and get a list of the strings contained within the foo's in Baz? The following mapping does not work

<mapping>
  <class-a>Baz</class-a>
  <class-b>Target</class-b>
  <field>
    <a>foos.string</a>
    <b>fooStrings</b>
  </field>
</mapping>

Because string is not a property of foos (which is of type List). I would have thought Dozer would be clever enough to, if it encountered a collection in a deep mapping, and the target was also a collection, to be able to break the deep property name into two and iterate across the collection to get the child part of the deep mapping from the collection members. Apparently not. Is there a solution short of making a feature request of Dozer?

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

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

发布评论

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

评论(3

独孤求败 2024-08-27 17:06:55

您始终可以编写自己的 CustomConverter

Dozer 无法按照您的预期处理此问题是有道理的,因为在运行时它没有有关 List foos 的类型信息,并且不能保证中的每个 Object该列表实际上是一个 Foo

You could always write your own CustomConverter.

It makes sense why Dozer isn't able to handle this as you expect since at runtime it has no type information about the List foos and can't guarantee that every Object in the list is actually a Foo.

夜司空 2024-08-27 17:06:55

我认为,您可以编写这样的映射

<mapping>
  <class-a>Baz</class-a>
  <class-b>Target</class-b>
  <field>
    <a>foos</a>
    <b>fooStrings</b>
  </field>
</mapping>

<custom-converters> 
  <converter type="CustomFooConverter">
    <class-a>
      Foo
    </class-a>
    <class-b>
      String
    </class-b>
  </converter>
</custom-converters>

并实现 CustomFooConverter 来获取 foo 的字符串字段并将其作为字符串返回。

我认为您可以发布一个功能请求来支持映射到基元,就像

<mapping>
  <class-a>Foo</class-a>
  <class-b>String</class-b>
  <field>
    <a>string</a>
  </field>
</mapping>

映射到 Dozer GitHub

I think, you can write such a mapping

<mapping>
  <class-a>Baz</class-a>
  <class-b>Target</class-b>
  <field>
    <a>foos</a>
    <b>fooStrings</b>
  </field>
</mapping>

<custom-converters> 
  <converter type="CustomFooConverter">
    <class-a>
      Foo
    </class-a>
    <class-b>
      String
    </class-b>
  </converter>
</custom-converters>

And implement CustomFooConverter to get string field of foo and return it as a String.

I think you can post a feature request to support mapping to primitives as

<mapping>
  <class-a>Foo</class-a>
  <class-b>String</class-b>
  <field>
    <a>string</a>
  </field>
</mapping>

into Dozer GitHub

花桑 2024-08-27 17:06:55

我认为你可以在没有自定义转换器的情况下做到这一点。

重写 Foo 类的 toString() 方法,如下所示:

@Override
public String toString(){
return this.getString(); //assuming string property has a getter method. if not,write this.string

现在是以下映射:

<mapping>
<class-a>fully qualified name of Baz(with package name)</class-a>
<class-b>same for Target</class-b>
<field>
   <a>foos</a>
   <b>fooStrings</b>
   <a-hint>foo</a-hint>
   <b-hint>java.lang.String</b-hint>
</field>
</mapping>

I think you can do it without custom converter.

Override the toString() method of Foo class like below:

@Override
public String toString(){
return this.getString(); //assuming string property has a getter method. if not,write this.string

And now the follwing mapping:

<mapping>
<class-a>fully qualified name of Baz(with package name)</class-a>
<class-b>same for Target</class-b>
<field>
   <a>foos</a>
   <b>fooStrings</b>
   <a-hint>foo</a-hint>
   <b-hint>java.lang.String</b-hint>
</field>
</mapping>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文