8

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 !

Community
  • 1
  • 1
Hyukchan Kwon
  • 382
  • 1
  • 5
  • 20
  • 3
    Have a look at Apache commons BeanUtils which uses reflection : http://commons.apache.org/proper/commons-beanutils/ – Arnaud Mar 24 '16 at 09:11
  • 2
    http://dozer.sourceforge.net/ – Tom Mar 24 '16 at 09:11
  • 2
    Possible duplicate of [How do I copy an object in Java?](http://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java) – M. Suurland Mar 24 '16 at 09:14
  • Berger and Tom, thanks , I'll have a loot at those links ! @hahn is my question a little bit stupid ? ^^' – Hyukchan Kwon Mar 24 '16 at 09:15
  • 1
    I agree it's not about copying an object but copying (partial) data between objects in different hierarchies. Dozer is a nice tool for that as it handles common properties (same name and type) automatically without having to write any additional line. – Thomas Mar 24 '16 at 09:24
  • @HyukchanKwon no no, I did not mean that question is stupid. There are gazillion of questions with answers about this topic. – hahn Mar 24 '16 at 09:29
  • @hahn Can you give me a link of one of those gazillion of questions that answers already my question ? I couldn't find one :/ – Hyukchan Kwon Mar 24 '16 at 09:34
  • @HyukchanKwon here are the results of a quick search: http://stackoverflow.com/questions/20370863/copy-pojo-fields-to-another-pojos-setters http://stackoverflow.com/questions/15117403/dto-pattern-best-way-to-copy-properties-between-two-objects http://stackoverflow.com/questions/4394978/copy-fields-between-similar-classes-in-java http://stackoverflow.com/questions/11900429/copying-one-classs-fields-into-another-classs-identical-fields http://stackoverflow.com/questions/19760590/how-to-copy-properties-from-a-bean-to-another-bean-in-different-class and some more.. – hahn Mar 24 '16 at 09:45
  • @hahn thanks ! I think I had difficulties finding those questions because when you search about copying objects in Java, you find a lot about those kind of questions : http://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java – Hyukchan Kwon Mar 24 '16 at 09:47

3 Answers3

7

Try libraries like Dozer or BeanUtils

bedrin
  • 4,458
  • 32
  • 53
6

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.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • That looks very close to what I'm looking for. Does Dozer handle parent attributes too ? Let's say now that Object A is extending an Object C which has an attribute a4 that I want to copy to attribute a4 of Object B ? – Hyukchan Kwon Mar 24 '16 at 09:33
  • @HyukchanKwon if ObjectB has attribute a4 as well then yes it should be copied. In most real world applications the mapping isn't that easy though, i.e. you might need to provide some code for the non-trivial mappings (like read-only, custom type conversion, different names etc.). – Thomas Mar 24 '16 at 09:34
  • Thanks @Thomas :) I'll give it a try ! – Hyukchan Kwon Mar 24 '16 at 09:35
  • I tried Doze and for basic mapping, it works really great with few lines of code ! Thanks :) – Hyukchan Kwon Mar 24 '16 at 09:53
1

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);
Rima
  • 545
  • 6
  • 12