Map objects of two different classes using Orika such that one class have additional attributes and auto increment id.

  1. Dozer
    Dozer is a mapping framework that uses recursion to copy data from one object to another.
  2. Orika
    Orika is a bean to bean mapping framework that recursively copies data from one object to another.The main difference between orika and dozer is that Orika uses bytecode generation. This allows for generating faster mappers with minimal overhead.
  3. MapStruct
    MapStruct is a code generator that generates bean mapper classes automatically.
  4. ModelMapper
    ModelMapper is a framework that aims to simplify object mapping, by determining how objects map to each other based on conventions. It provides type-safe and refactoring-safe API.
  5. JMapper
    JMapper aims to provide an easy-to-use, high-performance mapping between Java Beans. The framework aims to apply the DRY principle using Annotations and relational mapping.
public class Person{
private String name;
private int age;
// constructor and getter & setter methods
}

public class Student{
private long id;
private String studentName;
private int age;
private String country;
// constructor and getter and setter
}
<!-- https://mvnrepository.com/artifact/ma.glasnost.orika/orika-core -->
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.5.4</version>
</dependency>
final DefaultMapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();AtomicLong counter = new AtomicLong(1);mapperFactory.classMap(person.class, Student.class)
.field("name", "studentName")
.customize(new CustomMapper<Person, Student>() {
@Override
public void mapAtoB(Person person, Student student, MappingContext context)
{
student.setId(counter.getAndIncrement());
student.setCountry("india");
}
})
.byDefault()
.register();

final MapperFacade mapperFacade = mapperFactory.getMapperFacade();

// converting list of person object to list of student object

List<Person> personList = getPersonList();
List<Student> studentList = new ArrayList<>();
perosnList.forEach(p -> studentList.add( mapperFacade.map(p,Student.class)) );

--

--

--

System Administrator and Full stack web developer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

4A ECE— The Beginning Of The End

What is Gang Of Four (GOF)?

How to Access A Secure Elasticache Cluster?

TNG All-Hands Questions: 25January2019

How to Bulk Update Data and Import New Entities Quickly After Migration

LitExtension Smart Update

Dependency Injection for Unit Test

Flutter Login Animation

Microservices communication with Spring WebFlux

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Himanshu Pratap

Himanshu Pratap

System Administrator and Full stack web developer.

More from Medium

Auto Scale Azure SQL DB using Azure Logic Apps PowerShell Script and SQL Agent .

Calculate size of Root/Sub folders

Introduction to NodeRed and Simple Application

What is batch language?