0

The need to create an object clone came up at some point in our project and having heard that interfaces such as Cloneable and the like aren't really the best thing to use so as a fast solution I thought of just querying the database to receive a copy of that object (the moment the cloning happens is right after an upsert statement). Then I also thought of a hacky clone method using object serialization-deserialization. What would you choose?

Note that I am using MongoDB as a repository with Spring Data and in the future we are thinking of adding Spring's Caching mechanism in our repositories.

PentaKon
  • 4,139
  • 5
  • 43
  • 80
  • Why not a copy constructor? – Pau Aug 10 '16 at 10:43
  • The object has many fields that are other types which in turn have other types, many are Collections etc. If I implement a copy constructor I might as well implement a clone method. Also copy constructors are error prone to field additions. – PentaKon Aug 10 '16 at 11:17
  • Then I would get the copy from the database querying it as you suggest. Why not clone? -> http://stackoverflow.com/a/2599165/4751165 – Pau Aug 10 '16 at 11:34

0 Answers0