404-not-find / orika

Automatically exported from code.google.com/p/orika
2 stars 0 forks source link

User Guide code to map from List to Map throws ClassCastException #195

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

1. Create three classes as described in User Guide:

public class Name {
    private String first;
    private String last;
    private String fullName;
    //getters & setters
}

public static class Person {
    private List<Name> names;
    //getters & setters
}

public static class PersonDto {
    private Map<String, Name> personalNames;
    //getters & setters
}

2. Create mapping and facade as described in user guide:

mapperFactory.classMap(Person.class, PersonDto.class)
        .field("names{fullName}", "personalNames{key}")
        .field("names{}", "personalNames{value}")
        .register();

MapperFacade mapper = mapperFactory.getMapperFacade();

3. Attempt to map Person to PersonDto (as per User Guide)

PersonDto personDto = mapper.map(person, PersonDto.class);

What is the expected output? What do you see instead?

Each Name in List<Name> should be mapped to Map<Name.fullName, Name>
Instead a ClassCastException occurs:
    "java.lang.ClassCastException: java.lang.Object cannot be cast to <package>.Name"

What version of the product are you using? On what operating system?
Tested on both Orika 1.4.2 (as per User Guide) and Orika 1.4.5 (latest)
Windows-7, 64bit

Please provide any additional information below.
Testcase attached
Duplicate of issue 166 - which has not been Accepted

Original issue reported on code.google.com by jrandomu...@gmail.com on 30 Mar 2015 at 4:20

Attachments:

GoogleCodeExporter commented 8 years ago
Code works if an explicit mapper is added for Name (see attached code)
        mapperFactory.classMap(Name.class, Name.class)
                .byDefault()
                .register();

I would have thought this would not be necessary, and don't understand why it 
is.
Before closing this issue, can someone please explain either here - or 
preferably in the User Guide

Thanks

Original comment by jrandomu...@gmail.com on 31 Mar 2015 at 1:32

Attachments: