推土机深度映射设置< ComplexObject>设置< String> [英] Dozer deep mapping Set<ComplexObject> to Set<String>

查看:153
本文介绍了推土机深度映射设置< ComplexObject>设置< String>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:此处已经提出了同样的问题映射深层属性在推土机的中间收集,但它没有接受的答案(我的情况没有正确的答案)。



所以这个问题。我有一个由ComplexObject组成的领域,如下所示

Disclaimer: the same question has already been asked here Mapping deep properties with intermediate collections in dozer, but it has no accepted answer (and no proper answer for my case).

So the question. I have a realm composed by a ComplexObject like the following

public class ComplexObject {
  private Set<AnotherComplexObject> inner;
  ... //other fields, setters and getters
}
public Class AnotherComplexObject {
  private String property;
  ... //other fields, setters and getters
}

现在,我将 ComplexObject 映射到目标,其中Target有一个 Set< String> property。

Now, I am mapping ComplexObject to Target, where Target has a Set<String> property.

public class Target {
  private Set<String> targetString;
  ... //other fields, setters and getters
}

I想要将每个ComplexObject inner.property映射到一个Target targetString。在语义上看起来像的东西(当然,这不起作用,属性不是Set和Dozer的成员生成MappingException):

I want to map each ComplexObject inner.property onto one Target targetString. Something that semantically looks like (this does not work of course, property is not a member of Set and Dozer generates a MappingException):

<mapping>
  <class-a>ComplexObject</class-a>
  <class-b>Target</class-b>
  <field>
    <a>inner.property</a>
    <b>targetString</b>
  </field>
</mapping>



如果我修改<$ c $,我可以完成目标c> toString 的方法AnotherComplexObject

public class AnotherComplexObject {
  public String toString(){
    return property;
  }
}

然后,Dozer将检测源Set是键入AnotherComplexObject,而目标Set是String,并在转换过程中使用方法toString。不幸的是,这不是一个解决方案,因为我需要在我的POJO中使用一个方法让Dozer进行转换。



编写自定义转换器的工作是什么,它会覆盖convert方法以检查source是否为Set,然后假设集合中的对象是AnotherComplexObject并执行从这一点开始转换但不知何故,我觉得这不是最好的,也不是更优雅的解决方案。

关于如何解决这个问题的其他想法?

Then, Dozer will detect the source Set is of "type" AnotherComplexObject while the target Set is of String and will use the method toString during the conversion. Unfortunately, this is not quite a solution since I will need a method in my POJO just to let Dozer doing the conversion.

What does work is to write a custom converter which overrides the convert method to check if source is a Set and then supposes the objects in the set are AnotherComplexObject and doing the conversion from this point on but somehow I feel this is not best nor the more elegant solution.
Any other idea about how to solve this problem?

推荐答案

也许我的 answer 对您有用:

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

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>

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

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>

进入Dozer GitHub

这篇关于推土机深度映射设置&lt; ComplexObject&gt;设置&lt; String&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆