如何映射 List列表>与推土机?

发布于 2024-11-16 12:07:58 字数 1238 浏览 3 评论 0 原文

假设我有一个对象 Foo,它包含对对象 Bar 的引用列表:

public class Foo{
    String fooStr;
    List<Bar> barList;

    //get/set barList/someStr go here
}

public class Bar {
    String barStr;
}

使用 Dozer,假设 Bar 到 java.util.HashMap 是一个简单的映射,我如何将 Foo 的实例映射到 java.util 的实例。 HashMap 使得 Bar 的引用实例也映射到 java.util.HashMap? 也就是说,我希望映射的结果是一个带有键“barList”的 HashMap,其中包含一个实例数组列表>。此列表中的每个 HashMap 都应该是 Bar 到 HashMap 的映射。

例如,如果 Foo 持有对 Bar 而不是列表的单个引用,我会这样做:

<mapping>
    <class-a>Foo</class-a>
    <class-b>java.util.Map</class-b>

    <field>
        <a>bar</a>
        <b key="bar">this</b>
        <a-hint>Bar</a-hint>
        <b-hint>java.util.Map</b-hint>
    </field>
</mapping>

这将生成一个看起来像这样的哈希图(使用 JSON 对象来表示 HashMap):

{
    "fooStr" : "value of fooStr",
    {
       "barStr" : "value of barStr"
    }
}

但我想知道如何才能使用对 Bar 的引用列表表达对 HashMap 的转换,这样我就得到了:

{
    "fooStr" : "value of fooStr",
    "barList" : [{ "barStr" : "bar1" }, { "barStr" : "bar2" }, ...]
}

不使用自定义映射器的答案是首选,但我理解这是否是实现此行为的唯一方法。

Let's say I have an object Foo which holds a list of references to object Bar:

public class Foo{
    String fooStr;
    List<Bar> barList;

    //get/set barList/someStr go here
}

public class Bar {
    String barStr;
}

Using Dozer, assuming Bar to java.util.HashMap is a trivial mapping, how do I map instances of Foo to instances of java.util.HashMap such that the referenced instances of Bar are mapped to java.util.HashMap as well? That is, I want the result of the mapping to be a HashMap with key "barList" which holds an instance of ArrayList>. Each HashMap in this list should be a mapping of Bar to HashMap.

For instance, if Foo held a single reference to Bar rather than a list, I'd do:

<mapping>
    <class-a>Foo</class-a>
    <class-b>java.util.Map</class-b>

    <field>
        <a>bar</a>
        <b key="bar">this</b>
        <a-hint>Bar</a-hint>
        <b-hint>java.util.Map</b-hint>
    </field>
</mapping>

And this would produce a hashmap that'd look like this (using JSON objects to represent HashMaps):

{
    "fooStr" : "value of fooStr",
    {
       "barStr" : "value of barStr"
    }
}

But I want to know how I can express the conversion to HashMap with a list of references to Bar such that I get this:

{
    "fooStr" : "value of fooStr",
    "barList" : [{ "barStr" : "bar1" }, { "barStr" : "bar2" }, ...]
}

Answers which do not use a custom mapper are preferred, but I understand if this is the only way of achieving this behavior.

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

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

发布评论

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

评论(1

我很坚强 2024-11-23 12:07:58

您应该能够在自定义转换器中执行此操作:

public class Foo {
    String fooStr;
    List<Bar> barList;
    ...
}

public class Bar {
    String barStr;
}

public class Target {
    String fooStr;
    Map<String, Bar> barMap;   
}


public class TestCustomConverter implements CustomConverter {

  public Object convert(Object destination, Object source, Class destClass, Class sourceClass) {
    if (source == null) {
      return null;
    }
    if (source instanceof Foo) {
      Map<Bar> dest = null;
      // check to see if the object already exists
      if (destination == null) {
        dest = new Target();
      } else {
        dest = (Target) destination;
      }
      ((Target) dest).setFooStr(source.getFooStr());
      for(Bar : source.getBarList()) {
          ((Target) dest).getBarMap().put(bar.getBarStr(), bar);
      }
      return dest;
    } else if (source instanceof Target) {
      Foo dest = null;
      // check to see if the object already exists
      if (destination == null) {
        dest = new Foo ();
      } else {
        dest = (Foo) destination;
      }
      dest.getFoos().addAll(((Target)source).getBarMap().values());
      dest.setFooStr(((Target)source).getFooStr()):
      return dest;
    } else {
      throw new MappingException("Converter TestCustomConverter used incorrectly. Arguments passed in were:"
          + destination + " and " + source);
    }
  }

警告:代码可能包含错误,但应该为您提供想法。

You should be able to do this in a custom converter:

public class Foo {
    String fooStr;
    List<Bar> barList;
    ...
}

public class Bar {
    String barStr;
}

public class Target {
    String fooStr;
    Map<String, Bar> barMap;   
}


public class TestCustomConverter implements CustomConverter {

  public Object convert(Object destination, Object source, Class destClass, Class sourceClass) {
    if (source == null) {
      return null;
    }
    if (source instanceof Foo) {
      Map<Bar> dest = null;
      // check to see if the object already exists
      if (destination == null) {
        dest = new Target();
      } else {
        dest = (Target) destination;
      }
      ((Target) dest).setFooStr(source.getFooStr());
      for(Bar : source.getBarList()) {
          ((Target) dest).getBarMap().put(bar.getBarStr(), bar);
      }
      return dest;
    } else if (source instanceof Target) {
      Foo dest = null;
      // check to see if the object already exists
      if (destination == null) {
        dest = new Foo ();
      } else {
        dest = (Foo) destination;
      }
      dest.getFoos().addAll(((Target)source).getBarMap().values());
      dest.setFooStr(((Target)source).getFooStr()):
      return dest;
    } else {
      throw new MappingException("Converter TestCustomConverter used incorrectly. Arguments passed in were:"
          + destination + " and " + source);
    }
  }

Warning: Code may contain bugs, but should give you the idea.

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