In my entities I have an object Shelf with a collection of books
public class Shelf{
...
private List<Book> books;
}
In my ShelfDTO I only need to have the number of books
public class ShelfDTO{
...
private int booksNumber;
}
I've tried with:
modelMapper.addMappings(new PropertyMap<Shelf, ShelfDTO>() {
protected void configure() {
map().setBooksNumber(source.getBooks().size());
}
});
But I got an exception when I try to start the app :
Caused by: java.lang.NullPointerException: null
at org.modelmapper.internal.ExplicitMappingBuilder$ExplicitMappingInterceptor.access$000(ExplicitMappingBuilder.java:304) ~[modelmapper-2.3.0.jar:na]
at org.modelmapper.internal.ExplicitMappingBuilder.createAccessorProxies(ExplicitMappingBuilder.java:287) ~[modelmapper-2.3.0.jar:na]
at org.modelmapper.internal.ExplicitMappingBuilder.createProxies(ExplicitMappingBuilder.java:277) ~[modelmapper-2.3.0.jar:na]
Is there a way to do that with a property mapping or do I have to user a Converter?