Notifications
Clear all

[Solved] How do i copy a model object in java?

7 Posts
6 Users
0 Reactions
876 Views
0
Topic starter

I cant seem to find a "clone" method on classes that have been generated for my models in studio. What is the best way to shallow and deep copy?

2 Answers
0

MyModel clonedModel = (MyModel) JAXBUtil.clone(model);

Sergey Soshnikov 2013-03-15 03:03:00

there is a bunch of methods in platform to do that although they all down to marshal/unmarshal operations

Alexey Ulyanov 2013-03-15 03:03:00

Need to note that JAXBUtil.clone() doesn't copy sysIds fields.
So in some cases it cannot be used.

AllenWalsh 2013-09-03 16:09:00

What is the oldest version of platform that the clone method can be used with and when was it released?

0
Topic starter

Here is what i had come up with for a deep copy.

private MyModel deepCopyMyModel(MyModel old) {
  JSONObject json = old.toJSONObject();
  MyModel returnVal = Model.fromJSONObject(json, MyModel.class);
  return returnVal;
}
JoshuaReynolds 2015-10-06 10:10:00

This JSON marshall/unmarshall works on individual Child Models (OrderLine, ReceiptLine, etc). JAXB/XML fails on individual Child Models.