[英]Dozer: class cast exception while mapping lists
I have two value objects (ValueObjectA and ValueObjectB), each with name1 and name2 我有两个值对象(ValueObjectA和ValueObjectB),每个都有name1和name2
Then I have two lists, each holds one of the value objects, which I plan to map with dozer. 然后,我有两个列表,每个列表都包含一个值对象,我计划将其与推土机映射。
As soon as I access the mapped 'listOfB', I get a Class Cast Exception, since dozer maps objects of type ValueObjectA into the list of ValueObjectsB. 一旦我访问映射的“ listOfB”,就会得到一个类强制转换异常,因为推土机将ValueObjectA类型的对象映射到ValueObjectsB的列表中。
Is it possible to map these two lists without iterating the lists and map object by object? 是否可以映射这两个列表而无需迭代列表并逐对象映射?
sample code: 样例代码:
...
// prepare object A
List<ValueObjectA> lostOfA = new LinkedList();
ValueObjectA voA = new ValueObjectA();
voA.setName1("foo");
voA.setName2("bar");
lostOfA.add(voA);
// map object A to object B
List<ValueObjectB> listOfB = new LinkedList();
mapper.map(lostOfA, listOfB);
for (ValueObjectB voB:listOfB ){
...
Not easily. 不容易。
Take a look at this thread on the Dozer forum. 在推土机论坛上看看这个话题。
To quote: 报价:
"Nested collections are handled automatically, but you are correct that top level collections need to be iterated over. Currently there isn't a more elegant way to handle this." “嵌套的集合是自动处理的,但是您需要对顶级集合进行迭代,这是正确的。当前没有一种更优雅的方式来处理此问题。”
Try to define a mapping for both class . 尝试为两个类都定义一个映射。 Dozer will use this mapping at run-time automatically and convert objects accordingly. 推土机将在运行时自动使用此映射,并相应地转换对象。
for example (psudo code) : 例如(伪代码):
<mapping>
<classA>ValueObjectA</classA>
<classB>ValueObjectB</classB>
<mapping>
I guess the fields name in both class are same. 我猜这两个类中的字段名称都相同。 If not you need to specify them in the above mapping. 如果不是,则需要在上面的映射中指定它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.