Jayesh Dhandha
Jayesh Dhandha

Reputation: 2119

Dozer mapping with abstract class

I am using dozer to map between bean and model with annotation approach. Everything works well with most of the classes.

But recently i faced 1 issue when my bean has protected constructor with 1 parameter. Because this constructor is being used in some places with super keyword.

Below is Sample example:

TestBean.java:

abstract public class TestBean{

  public enum Type {
    ALL, FIELD, QUERY, HIERARCHY
  }

  public enum Sort {
    COUNT, INDEX, VALUE
  }

  private Type type;
  private String description;
  private Sort sort;
  
  public FacetBean() {
  }

  protected FacetBean(FacetBean from) {
    setId(from.getId());
    description = from.description;
    sort = from.sort;  
  }

  public Type getType() {
    return type;
  }

  public void setType(Type type) {
    this.type = type;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public Sort getSort() {
    return sort;
  }

  public void setSort(Sort sort) {
    this.sort = sort;
  }
}

Below is the sample code for my model.

TestModel.java

public class TestModel{

  public enum Type {
    ALL, FIELD, QUERY, HIERARCHY
  }

  public enum Sort {
    COUNT, INDEX, VALUE
  }

  private Type type;
  private String description;
  private Sort sort;
  

  public Type getType() {
    return type;
  }

  public void setType(Type type) {
    this.type = type;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public Sort getSort() {
    return sort;
  }

  public void setSort(Sort sort) {
    this.sort = sort;
  }
}

I am using org.dozer.mapper to map between model and bean.

Below is working code:

mapper.map(testBean, TestModel.class) --> WORKING

If i try the otherway round, then it's giving me mapper.map(testModel, TestBean.class) --> NOT WORKING

org.dozer.MappingException: java.lang.InstantiationException

Any idea how to solve this issue? (I can't change TestBean.java as it is very old class and being used in many places, I have created TestModel.java recently to map the data of TestBean.java to it and return as API response)

Thanks!

Upvotes: 0

Views: 457

Answers (0)

Related Questions