[英]Dozer deep mapping Set<ComplexObject> to Set<String>
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). 免责声明:这里已经提出了同样的问题。 在dozer中使用中间集合映射深层属性 ,但它没有被接受的答案(对我的情况没有正确答案)。
So the question. 所以问题。 I have a realm composed by a ComplexObject like the following
我有一个由ComplexObject组成的领域,如下所示
public class ComplexObject {
private Set<AnotherComplexObject> inner;
... //other fields, setters and getters
}
public Class AnotherComplexObject {
private String property;
... //other fields, setters and getters
}
Now, I am mapping ComplexObject
to Target
, where Target has a Set<String>
property. 现在,我将
ComplexObject
映射到Target
,其中Target具有Set<String>
属性。
public class Target {
private Set<String> targetString;
... //other fields, setters and getters
}
I want to map each ComplexObject inner.property onto one Target targetString. 我想将每个ComplexObject inner.property映射到一个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):
在语义上看起来像的东西(当然,这不起作用,属性不是Set和Dozer的成员生成MappingException):
<mapping>
<class-a>ComplexObject</class-a>
<class-b>Target</class-b>
<field>
<a>inner.property</a>
<b>targetString</b>
</field>
</mapping>
I can accomplish my goal if I modify the toString
method of AnotherComplexObject
to 如果我将
AnotherComplexObject
的toString
方法修改为,我可以实现我的目标
public class AnotherComplexObject {
public String toString(){
return property;
}
}
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. 然后,Dozer将检测源Set是“type”AnotherComplexObject,而目标Set是String,并将在转换期间使用方法toString。 Unfortunately, this is not quite a solution since I will need a method in my POJO just to let Dozer doing the conversion.
不幸的是,这不是一个解决方案,因为我需要一个方法在我的POJO中让Dozer进行转换。
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. 编写一个自定义转换器是什么工作,它会覆盖convert方法以检查source是否为Set,然后假设集合中的对象是AnotherComplexObject并从这一点开始进行转换但不知何故我觉得这不是最好也不是更多优雅的解决方
Any other idea about how to solve this problem? 关于如何解决这个问题的任何其他想法?
Maybe my answer can be useful for you: 也许我的回答对你有用:
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. 并实现CustomFooConverter以获取foo的字符串字段并将其作为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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.