How to map an abstract class List attribute with mapstruct

32 views Asked by At

I have a DTO that looks like this:

@Getter @Setter @AllArgsConstructor
public class PsychologicalSupportProcessDTO {
    protected String id;
    protected UserDTO user;
    protected ArrayList<AbstractQuestionnaireDTO> questionnaires;

    public PsychologicalSupportProcessDTO() {

    }
}

And a mapper that I define and use like this:

@Mapper(componentModel = "spring")
public interface PsychologicalSupportProcessMapper {
    List<PsychologicalSupportProcessDTO> toDtoList(List<PsychologicalSupportProcess> psp);
}

In my service I call the mapper like this:

@Override
    public List<PsychologicalSupportProcessDTO> findAll() {
        List<PsychologicalSupportProcess> psychologicalSupportProcess = this.questionnaireService.findAll();
        List<PsychologicalSupportProcessDTO> psychologicalSupportProcessDTO = psp.toDtoList(psychologicalSupportProcess);
        return psychologicalSupportProcessDTO;
    }

If I comment on the protected ArrayList<AbstractQuestionnaireDTO> questionnaires attribute; Mapping works well. But if I set it I get the following errors:

  1. java: The return type AbstractQuestionnaireDTO is an abstract class or interface. Provide a non abstract / non interface result type or a factory method.
  2. Consider to declare/implement a mapping method: "ArrayList map(ArrayList value)".

I understand that being an abstract class it should indicate which of the implementations to use, but I don't understand the documentation very well. My abstract class looks like this and the 2 implementations I have:

@Getter @Setter @AllArgsConstructor
public abstract class AbstractQuestionnaireDTO {
    protected String name;
    protected Integer score;
    protected AbstractQuestionnaireDTO(){

    }
}
    @Getter @Setter @AllArgsConstructor
public class PsyFactorsExtraDTO extends AbstractQuestionnaireDTO {
    private String name;
    private Integer score;
    public PsyFactorsExtraDTO(){
    }
}
    @Getter @Setter @AllArgsConstructor
public class PsyFactorsIntraDTO extends AbstractQuestionnaireDTO{
    private String name;
    private Integer score;
    public PsyFactorsIntraDTO(){
    }
}

I should have a mapper for each DTO? it is also not very clear to me when I should have a mapper. Thank you in advance for your help

0

There are 0 answers