Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA - How to copy attributes of an object to another object having the same attributes?

Tags:

java

object

copy

Let's say we have an Object A defined like this:

public class ObjectA {
    private Attribute a1;
    private Attribute a2;
    private Attribute a3;
}

For some reason, I need to create a second object B with only the first two attriutes of the Object A :

public class ObjectB {
    private Attribute a1;
    private Attribute a2;
}

So my question is: what is the best approach to copy an Object A to an Object B ? I've been copying the attributes by getters and setters one by one but something tells me there must be a better way to do this ! Especially when the object will have a lot of attributes, I have to write lines and lines of code just to copy all of them to the second Object B ...

Thanks a lot :)

EDIT: I've been being alerted by a "possible duplicate of another question" : How do I copy an object in Java?

My question is slightly different in a way that I'm dealing with 2 different objects who just share the same attributes but not totally !

like image 683
Hyukchan Kwon Avatar asked Mar 24 '16 09:03

Hyukchan Kwon


People also ask

How can we move the data from one object to another object?

Copy/Move Data If you only need to copy or move data to another object, you will need to: Export the data from the original object: Export Records. Import the data into the other object: Import Records.

Which is the process of copying the values of one object into another?

Example of clone() method (Object cloning) As you can see in the above example, both reference variables have the same value. Thus, the clone() copies the values of an object to another. So we don't need to write explicit code to copy the value of an object to another.


3 Answers

To expand on my comment:

Using Dozer it can be as easy as:

Mapper mapper = new DozerBeanMapper();
ObjectA source = new ObjectA();
ObjectB target = mapper.map(source , ObjectB.class);

or if your target class doesn't have a no-arg constructor:

ObjectA source = new ObjectA();
ObjectB target = new ObjectB(/*args*/);
mapper.map(source, target );

From the Documentation (emphasis by me):

After performing the Dozer mapping, the result will be a new instance of the destination object that contains values for all fields that have the same field name as the source object. If any of the mapped attributes are of different data types, the Dozer mapping engine will automatically perform data type conversion.

like image 61
Thomas Avatar answered Nov 09 '22 03:11

Thomas


Try libraries like Dozer or BeanUtils

like image 20
bedrin Avatar answered Nov 09 '22 03:11

bedrin


What you need is Object mappers. Try Orika or Dozer. The objects need not be of the same type. While mapping if it finds the attributes of the same name and type, it automatically maps it.

MapperFacade mapper = mapperFactory.getMapperFacade();
UserDTO userDTO = new UserDTO();
userDTO.setName("xyz");
..
User user = mapper.map(userDTO, User.class);

You can also customize if you have to map different attribute names.

MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(UserDTO.class, User.class)
            .field("name", "username")
            .byDefault().register();
mapper = mapperFactory.getMapperFacade();
...
User user = mapper.map(userDTO, User.class);
like image 43
Rima Avatar answered Nov 09 '22 03:11

Rima