1

What is the best way if I want to copy a model object in java. Because writing copy() function for a nested object in java becomes a lot of works. I just want to avoid that. As a shortcut I use this approach.

    public static <T> T copy(T model, Class<T> tClass) throws Exception {
        final ObjectMapper objectMapper = new ObjectMapper();
        final byte[] bytes = objectMapper.writeValueAsBytes(model);
        final T copy = objectMapper.readValue(bytes, tClass);
        return copy;
    }

And use it like this.

    final McTrack copy = copy(new McTrack(), McTrack.class);

I have made a Utility function copy() that takes a model object and returns a copy of that. First I serialize the entire object into json and then deserialize it again to make a copy. But I am not sure it really efficient. Is there any better way to copy plain old java objects.

sohan nohemy
  • 615
  • 5
  • 13

4 Answers4

3

you can use method:

BeanUtils.copyProperties(aValue, aLocal);

in apache commons-beanutils jar

click here commons-beanutils visit apache document

lynnsea
  • 137
  • 1
  • 9
2

You can also use Dozer library: http://dozer.sourceforge.net/

You can easily copy whole POJOs from one to another like this:

Mapper mapper = new DozerBeanMapper();
DestinationObject destObject = mapper.map(sourceObject, DestinationObject.class);

What is really usufull you can configure your own mappings like this:

<mapping>
  <class-a>yourpackage.yourSourceClassName</class-a>
  <class-b>yourpackage.yourDestinationClassName</class-b>
    <field>
      <a>yourSourceFieldName</a>
      <b>yourDestinationFieldName</b>
    </field>
</mapping> 

Additionally you can use different Technics to copy object, use custom factories, special getters/setters and a lot useful things: http://dozer.sourceforge.net/documentation/mappings.html

KirkoR
  • 788
  • 6
  • 13
1

You could use Object.clone(). There are some arguments against it but it's usable. Beware, this is a shallow clone. It depends on the class you are cloning, if a shallow clone will suffice.

See also here for deep clone recommendations: Deep clone utility recomendation

Community
  • 1
  • 1
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
0

Apart from the once mentioned above, you can also try ModelMapper. No configuration is required. In cases where it is important, there are 3 matching strategies you can use.

ModelMapper modelMapper = new ModelMapper();
OrderDTO orderDTO = modelMapper.map(order, OrderDTO.class);

This is a library I (and my team) used for a very big project and was very convenient. I attach the getting started page here that explains things better.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Aman
  • 1,627
  • 13
  • 19
  • 2
    it does not copy the object, order and orderDTO are seen same object. – Halil İbrahim Oymacı Oct 14 '21 at 09:35
  • Check if all fields names and types are the same, since matching strategy needs it. It is a well tested and widelly used library, so may be better to check on your side Halil. And is better in performance than the accepted – Aman Oct 14 '21 at 13:27
  • As @HalilİbrahimOymacı said, this does not copy object (at least not by default). I had to set **modelMapper.getConfiguration().setDeepCopyEnabled(true);** to make a real copy of source object. – Piro Jul 20 '22 at 06:12