How to map entities with private final collections using ModelMapper

Viewed 12

ModelMapper is a very good and useful tool but sometimes the requirement of using a set method is very inconvinient

follow the example

    class A {
    private final Collection<String> colA;

   public Collection<String> getColA(){
    return colA;
}
    }
    
    class B {
    
    private final Collection<String> ColB;
    
    public Collection<String> getColB(){
    return ColB;
}
    }

Very simple example, how can I use model mapper to convert between these two classes?

In my head it should be very simple doing something like:

  modelMapper.typeMap(A.class, B.class)
          .addMappings(new PropertyMap<>() {
             @Override
             protected void configure() {
                destination.getColB().addAll(source.getColA());
             }
          });

But by doing this the program crashes right after start with some modelmapper complaints.

How can I achieve this?

0 Answers
Related