Search code examples
javaspring-bootmodelmapper

List displays its precedent value


So I am using a modelmapper to convert all my entities, worked fine until now. I have an entity TaskDTO with a List of Double in its attributes. I can recuperate this list but once a request is made, all next requests get me the same list.

Here is the part for my converter:

public static TaskDTO converTaskToDTO(Task source) {
    List<Double> icList = new ArrayList<>();
    for (ChargeInitial c : source.getCharge()) {
        icList.add(c.getCharge());
    }
    System.out.println(icList);
    TypeMap<Task, TaskDTO> typeMap = modelMapper.getTypeMap(Task.class, TaskDTO.class);
    if (typeMap == null) {

        modelMapper.addMappings(new PropertyMap<Task, TaskDTO>() {
            @Override
            protected void configure() {
                map().setLot(source.getLot().getName());
                map().setProject(source.getLot().getProject().getName());
                map().setCollaborator(source.getCollaborator().getEmail());
                map().setCharge(icList);

            }
        });
    }

    return modelMapper.map(source, TaskDTO.class);

}

However the sysout prints the right list for me.

For exemple if in the first request I get charge[1,2] for that list, the second request will be also [1,2] for every charge list even though I get the right esult which is [1,2,3,4,5] in my system.out.

What could be the problem?

EDIT

I changed the method setCharge in TaskDTO as follows, works fine.

public void setCharge(List<ChargeInitial> charge) {
    List<Double> list = new ArrayList<>();
    for (ChargeInitial c : charge) {
        list.add(c.getCharge());
    }
    this.charge=list;
}

Solution

  • You can use Converter, List<ChargeInitial> to List<Double>.

    public static TaskDTO converTaskToDTO(Task source) {
    
        TypeMap<Task, TaskDTO> typeMap = modelMapper.getTypeMap(Task.class, TaskDTO.class);
        if (typeMap == null) {
    
            PropertyMap<Task, TaskDTO> mapping = new PropertyMap<Task, TaskDTO>() {
                @Override
                protected void configure() {
                    map().setLot(source.getLot().getName());
                    map().setProject(source.getLot().getProject().getName());
                    map().setCollaborator(source.getCollaborator().getEmail());
                }
            };
            modelMapper.addMappings(mapping);
    
            Converter<List<ChargeInitial>, List<Double>> chargeInitialConverter = new AbstractConverter<>() {
                @Override
                protected List<Double> convert(List<ChargeInitial> source) {
                    List<Double> icList = new ArrayList<>();
                    for (ChargeInitial c : source) {
                        icList.add(c.getCharge());
                    }
                    System.out.println(icList);
                    return icList;
                }
            };
            modelMapper.addConverter(chargeInitialConverter);
    
        }
    
        return modelMapper.map(source, TaskDTO.class);
    
    }