Match between two objects that contain a list using Orika

I'm trying to use Orika to match between two objects that contain List<...>where the List type is another object, despite the fact that despite trying to rearrange the display configuration in mapperFactory.classMap(...), Orika throws an exception when my program starts.

Looking at http://orika-mapper.imtqy.com/orika-docs/mappings-via-classmapbuilder.html , it seems that this syntax for matching a Listshould be parentProperty{childProperty}.

For the purposes of this question, I have simplified the objects that I am trying to display. The source object is ToDoTaskListEntity, and the target object ToDoTaskListDTO. The source object ToDoItemEntitycontains a list that is defined as List<ToDoItemEntity>, and the target object contains a corresponding list that is defined asList<ToDoItemDTO>

My question is how to define the display configuration in Orika between ToDoTaskListEntityand ToDoTaskListDTOso that the child is List<ToDoItemEntity>also displayed in List<ToDoItemDTO>in their respective parent objects


The code for my display configuration is as follows:

MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

mapperFactory.classMap(ToDoItemEntity.class, ToDoItemDTO.class)
    .field("id", "id")
    .field("description", "description")
    .field("status", "status")
    .byDefault()
    .register();

mapperFactory.classMap(ToDoTaskListEntity.class, ToDoTaskListDTO.class)
    .field("listName", "listName")
    .field("toDoItems{id}", "toDoItems{id}")
    .field("toDoItems{description}", "toDoItems{description}")
    .field("toDoItems{status}", "toDoItems{status}")
    .byDefault()
    .register();

The code for invoking the display is as follows:

MapperFacade mapper = mapperFactory.getMapperFacade();
ToDoTaskListDTO toDoTaskListDTO = mapper.map(toDoTaskListEntity, ToDoTaskListDTO.class);

The code for my source objects is as follows:

public class ToDoTaskListEntity {

    private String ListName;
    private List<ToDoItemEntity> toDoItems;

    // Getters and setters
}

public class ToDoItemEntity {

    private int id;
    private String description;
    private Status status;

    // Getters and setters
}

The code for my targets is as follows:

public class ToDoTaskListDTO {

    private String listName;
    private List<ToDoItemDTO> toDoItems;

    public ToDoTaskListDTO(String listName, List<ToDoItemDTO> toDoItems) {
        this.listName = listName;
        this.toDoItems = toDoItems;
    }

    //Getters but no setters

}

public class ToDoItemDTO {

    private int id;
    private String description;
    private Status status;

    public ToDoItemDTO(int id, String description, Status status) {
        this.id = id;
        this.description = description;
        this.status = status;
    }

    // Getters but no setters
}

The exception that Orika chose is the following:

Exception in thread "main" ma.glasnost.orika.MappingException: exception while creating object factory for test.orikademo.ToDoTaskListDTO
-----begin dump of current state-----------------------------
Registered object factories: 1 (approximate size: 25.4 kB)
  [ToDoTaskListDTO] : {}
-------------------------------------------------------------
Registered mappers: 2 (approximate size: 961.5 kB)
  [0] : GeneratedMapper<ToDoItemEntity, ToDoItemDTO> {usedConverters: [], usedMappers: [], usedMapperFacades: [], usedTypes: [] }
  [1] : GeneratedMapper<ToDoTaskListEntity, ToDoTaskListDTO> {usedConverters: [], usedMappers: [], usedMapperFacades: [DefaultBoundMapperFacade<ToDoItemEntity, ToDoItemDTO>], usedTypes: [List<ToDoItemEntity>, List<ToDoItemDTO>] }
-------------------------------------------------------------
Registered concrete types: 5 (approximate size: 122.4 kB)
  [interface java.util.List] : ArrayList<Object>
  [interface java.util.Set] : LinkedHashSet<Object>
  [interface java.util.Collection] : ArrayList<Object>
  [interface java.util.Map$Entry] : MapEntry<Object, Object>
  [interface java.util.Map] : LinkedHashMap<Object, Object>
-------------------------------------------------------------
Resolved strategies: 0 (approximate size: 0.8 kB)
-------------------------------------------------------------
Unenhance strategy: ma.glasnost.orika.unenhance.BaseUnenhancer@292a74d5
-----end dump of current state-------------------------------
    at ma.glasnost.orika.impl.generator.ObjectFactoryGenerator.build(ObjectFactoryGenerator.java:110)
    at ma.glasnost.orika.impl.DefaultMapperFactory.lookupObjectFactory(DefaultMapperFactory.java:1005)
    at ma.glasnost.orika.impl.DefaultMapperFactory.lookupObjectFactory(DefaultMapperFactory.java:925)
    at ma.glasnost.orika.impl.MapperFacadeImpl.resolveMappingStrategy(MapperFacadeImpl.java:218)
    at ma.glasnost.orika.impl.MapperFacadeImpl.map(MapperFacadeImpl.java:734)
    at ma.glasnost.orika.impl.MapperFacadeImpl.map(MapperFacadeImpl.java:714)
    at test.orikademo.App.main(App.java:54)
Caused by: java.lang.NullPointerException
    at ma.glasnost.orika.impl.generator.ObjectFactoryGenerator.addSourceClassConstructor(ObjectFactoryGenerator.java:173)
    at ma.glasnost.orika.impl.generator.ObjectFactoryGenerator.addCreateMethod(ObjectFactoryGenerator.java:124)
    at ma.glasnost.orika.impl.generator.ObjectFactoryGenerator.build(ObjectFactoryGenerator.java:95)
    ... 6 more
+4
1

Orika Google Groups , .

- , DTO

MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
// item class map
mapperFactory.classMap(ToDoItemEntity.class, ToDoItemDTO.class)
    .constrcutorB("id", "description", "status")
    .byDefault()
    .register();

mapperFactory.classMap(ToDoTaskListEntity.class, ToDoTaskListDTO.class)
    .constructorB("listName", "toDoItems")
    .byDefault()
    .register();

, toDoItems {id} ( ) , , (, ), Orika : .

+2

All Articles