简体   繁体   中英

Dozer: class cast exception while mapping lists

I have two value objects (ValueObjectA and ValueObjectB), each with name1 and 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM