Re: Duplicating an EO and all its relationships
Re: Duplicating an EO and all its relationships
- Subject: Re: Duplicating an EO and all its relationships
- From: Chuck Hill <email@hidden>
- Date: Thu, 31 Jul 2003 15:35:27 -0700
- Organization: Global Village Consulting, Inc.
And it can get more complicated than that. :-) You might not want
some of the related objects copied by reference as Art suggests below.
In that case you will need to create a new copy of the related
object and the whole process starts all over again.
Gary Tetter has previously posted code to do a reference/deep copy
based on information in the EOModel. I think that was on the Omni
list, it should be in the archives. However, this requires yours
objects to be model "just so" and that is not always the case. At
least in my experience.
In which case, as Art says, "this will likely be more than just a few
lines of code". It is not a trivial problem to copy part of an object
graph. The solution is also not trivial in the general case.
Chuck
Art Isbell wrote:
On Thursday, July 31, 2003, at 08:49 AM, Albert Jagnow wrote:
What I am trying to do is to create a duplicate copy of an EO including
all the relationship objects and save the copies to the database. For
example I have a purchase request EO that has many purchase items, and
each item may have many accounts/grants associated with it (it is
slightly more complicated than this but that is the basic structure).
What I want to do is create a copy of the entire purchase request that I
can save to the database. Everything would be the exactly the same in
the copy except for of course for the primary and foreign keys (which I
will let EOF create), and some timestamps, and descriptions which I
would set. This seems like it should be very easy, like only a few
lines of code, but I couldn't find an easy way to do it. Anyone have
any suggestions?
The standard Java approach would be to implement clone() in the EO
class. But for good reasons, EOGenericRecord, the EO superclass, does
not implement the Cloneable interface, so its private instance variables
aren't going to be correctly copied.
To copy an EO instance, you'll need to create a new instance, insert
it into an EC as always, then for each attribute, send the new object a
setter message whose argument is the value returned by sending the
original object a getter message. For each relationship, send the new
object an addObjectToBothSidesOfRelationshipWithKey() message the first
of whose arguments is the value returned by sending the original object
a getter message.
So this will likely be more than just a few lines of code.
Aloha,
Art
--
Chuck Hill email@hidden
Global Village Consulting Inc. http://www.global-village.net
Progress is the mother of all problems.
- G. K. Chesterton
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.