Search code examples
apache-commonsapache-commons-beanutilsjodd

BeanUtil does not copy data between 2 objects


In Jodd BeanUtil class does not have a method that will copy data from one object to another, i.e. in apache commons BeanUtils class there is a method copyProperties that will copy data from one object to another.

In Jodd we have to pass the name of the field and its value. If there are like 20+ fields do we have to do this manually for all the 20 fields or is there a better way of doing it using Jodd BeanUtil.


Solution

  • Actually, there is such tool in Jodd - its just in different class: BeanCopy (javadoc). This class offers some more control over copy process, like including/exluding some properties and so on. It was too big to put all this functionality in the BeanUtil :)

    Take a look at the test sources, you will find many examples there. Here are some quick examples:

    BeanCopy.beans(src, dest).copy();
    BeanCopy.beans(src, dest).include("fooInteger", "fooLong").copy();
    BeanCopy.beans(fooBean, dest).includeAs(FooBeanString.class).copy();
    BeanCopy.beans(src, dest).declared(true).copy();
    

    and so on.