springjaxbentitydtoapache-commons-beanutils

Why it seems impossible to use BeanUtils.copyProperties from a JPA entity to a JAX-B Bean?


We are using JPA Entities to get the database rows and then when we transfer that to the external, we want to use disconnected object (DTO) which are simple beans annotated with JAX-B.

We use a mapper and its code looks like this:

public BillDTO map(BillEntity source, BillDTO target) {
    BeanUtils.copyProperties(source, target);
    return target;
}

But when the code is running we get an error like this:

java.lang.IllegalArgumentException: argument type mismatch

Note this is the Spring implementation of the BeanUtils:

import org.springframework.beans.BeanUtils

And the naming of the properties are identical (with their getter/setter).


Solution

  • This example working well. Here String property is copied to enum property:

    Entity:

    public class A {
       private String valueFrom;
    
       public String getValue() {
          return valueFrom;
       }
    
       public void setValue(String value) {
          this.valueFrom = value;
       }
    }
    

    DTO (En is enumeration):

    public class B {
       private En valueTo;
    
       public void setValue(String def) {
          this.valueTo = En.valueOf(def);
       }
       
       public void setEnumValue(En enumVal) {
          this.valueTo = enumVal;
       }
    }
    

    As for your GitHub example, problem in class B in getter should be:

    public String getValue()
    

    Example:

    public String getValue() {
       return value.toString();
    }