copy one model class to another model class, class A and class B different properties name, want to set the class A properties to class B with manual mapping of field.
Asked
Active
Viewed 734 times
-1
-
just like BeanUtils.copyProperties but field mapping is choice. – Abhishek Maurya Jul 12 '22 at 11:19
-
Have you checked `ModelMapper` and `DozerMapper`? Check this [DozerMapper Link](http://modelmapper.org/user-manual/property-mapping/) – Rishal Jul 12 '22 at 11:58
-
use apache's `BeanUtils.populate` – Ashish Patil Jul 12 '22 at 12:01
-
Please provide a code. You should have tried to write some code already – HoRn Jul 12 '22 at 17:43
1 Answers
0
ModelMapper provides multiple functionality, As per your requirement you can explore it out.
Model Mapper Documentation Link
Giving a short example as below:
public class POJO {
public static void main(String[] args) {
ModelMapper mapper = new ModelMapper();
TypeMap<Source, Destiation> typemap = mapper.createTypeMap(Source.class,Destiation.class);
typemap.addMappings(mappings -> mappings.map(Source::getName,Destiation::setNom));
typemap.addMappings(mappings -> mappings.map(Source::getLastName,Destiation::setPrenom));
Source source = new Source();
source.setName("John");
source.setLastName("Doe");
Destiation destiation = mapper.map(source,Destiation.class);
System.out.println(destiation.toString());
}
}
@Data //lombok annotation
class Source{
private String name;
private String lastName;
}
@Data //lombok annotation
class Destiation{
private String nom;
private String prenom;
}

Rishal
- 1,480
- 1
- 11
- 19