12

I'm working with mongodb so I'm decoupling entities from presentation layer creating DTOs (with hibernate-validator annotations).

public abstract class UserDTO {

    private String id;      
    @NotNull
    protected String firstName;
    @NotNull
    protected String lastName;
    protected UserType type;
    protected ContactInfoDTO contact;
    protected List<ResumeDTO> resumes;

    public UserDTO(){}
    //...

I'm trying to retrive from db this concrete class

public class UserType1DTO extends UserDTO {

    private CompanyDTO company;

    public UserType1DTO(){
        super();
    }

    public UserType1DTO(String firstName, String lastName, ContactInfoDTO contact, CompanyDTO company) {
        super(UserType.type1, firstName, lastName, contact);
        this.company = company;
    }
    /...

Like this:

return mapper.map((UserType1) entity,UserType1DTO.class);

And I get this error about not being able to instanciate ResumeDTO

Failed to instantiate instance of destination *.dto.ResumeDTO. Ensure that *.dto.ResumeDTO has a non-private no-argument constructor.

ResumeDTO is similar to UserDTO, is an abstract class and has concrete classes for each user type. All they have constructors with no arguments. What is the problem?

1

4 Answers 4

6

You are trying to map a concrete class to an abstract class, this will not work. You can not use as destination an Abstract Class. Why? It can not be instantiated. So you must use a concrete class.

Definitively it wouldn't work a map with an Abstract Class destination:

mapper.map(entity, AbstractClass.class);
/*Error: java.lang.InstantiationException
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)*/

You must use a concrete class which extends the Abstract Class

public class ConcreteClass extends AbstractClass {
       //
}

And then map it to this concrete class:

mapper.map(entity, ConcreteClass.class);

More info:

Due to it is not possible to instantiate an abstract class it will not work in destination properties neither.

There is an issue in Github related to this: https://github.com/jhalterman/modelmapper/issues/130

4
  • That's what I'm doing, isnt? return mapper.map((UserType1) entity,UserType1DTO.class); UserType1 and UserType1DTO are concrete classes.
    – anat0lius
    Commented Mar 2, 2017 at 11:42
  • What error exactly? Notice that I have a ResumeDTO list attribute on my abstract class and the error I get is about this attribute Failed to instantiate instance of destination *.dto.ResumeDTO. Ensure that *.dto.ResumeDTO has a non-private no-argument constructor is not telling me nothing me about not able to instanciate abstract classes
    – anat0lius
    Commented Mar 2, 2017 at 11:54
  • Yes, you're right. But, you say: ResumeDTO is similar to UserDTO, is an abstract class. So definetively you're problem is related with the abstract class, it can not be mapped (ResumeDTO, cannot be mapped because is an abstract class)
    – Pau
    Commented Mar 2, 2017 at 11:57
  • Aaaah, yes. I see it now. Then I can't do the map having this model :( I've tried MapStruct before ModelMapper and it didn't work neither
    – anat0lius
    Commented Mar 2, 2017 at 12:00
1

This error occurs when you have primitive data types or primitive return types in setter and getter or a parameterized constructor

so here you need to remove following code

public UserType1DTO(String firstName, String lastName, ContactInfoDTO contact, 
CompanyDTO company) {
    super(UserType.type1, firstName, lastName, contact);
    this.company = company;
}

it will work fine.

1
  • But I don't see a primitive type in this constructor. How will removing this constructor solve the issue? Commented Jan 27, 2021 at 8:04
1

What solved my issue is using typeMap and updating the version of ModelMapper. Please refer the below link:-

Mapping Lists with ModelMapper

Using typeMap still gave me the same error. Then I updated my ModelMapper version from 2.0.0 to 2.3.5, and the issue was resolved.

0

I was also getting the same exception. The way i resolved is made a private constructor of the destination class.

ViewDTO viewDto = (new ModelMapper()).map(object, ViewDTO.class);

The "object" is the domain object and it is to be mapped to the viewDto which is a DTO object to be sent to the client side.

I just added a private constructor to the ViewDTO class.

private ViewDTO() {}

and this solved my issue.And the verified answer also helped a lot.

Hope this is helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.