Looks good to me. Best to avoid detaching. Note how easy it was to
write the copying code. Sorry for not responding earlier, I missed it.
Anthony
At 02:09 AM 24-11-07, John Abraham wrote:
>I have a need to copy a record in a database, and then modify the
>original and the copy. I make a new record using
>
>ParcelORM newOne = (ParcelORM) ParcelORM.meta.createWithGeneratedKey();
>then I want to copy all of the fields except the key field (which has
>been generated).
>
>I can't use clone() because it won't work once I'm attached. So I'm
>proceeding as shown below, and would appreciate any feedback.
>
>First I wrote code in my user object to copy the fields:
>
>for (int i=0;i<fieldValues.length;i++) {
>if (!meta.isKeyField(i)) {
>fieldValues[i]= source.fieldValues[i];
>}
>}
>setDirty();
>
>The I added this method to SRecordMeta:
>
>public boolean isKeyField(int i) {
>for (int k=0;k<keySFieldMetas.size();k++) {
>SFieldMeta s = (SFieldMeta) keySFieldMetas.get(k);
>if (s.fieldIndex==i) return true;
>}
>return false;
>}
>
>}
>
>So I'm copying all the fields that aren't key fields. The foreign key
>fields might still be a problem, I don't have any foreign keys so it's
>hard to know.
>
>Another approach would be to detach the original, use the deprecated
>clone() method, then try to reattach the original and attach the copy.
>But that seems overly complex.
>
>--
>John Abraham
><mailto:jabraham%40ucalgary.ca>jabraham@...
>
EMail: berglas@...
+61 7 3369 4065 (Australian Eastern Standard Time)
If the model seems correct only because the numbers look right,
then why build the model in the first place?