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 16:01:16 -0700
- Organization: Global Village Consulting, Inc.
Briefly, and off the top of my head,
Taking (my interpretation of) Albert's example, we have a complex PO
that we want to use as a template for a new, very similar PO. We want
to copy the the PO and PO items as brand new objects.
- using addObjectWithReallyLongName ;-) will create a new PO with
references to the items from the previous PO. Almost certainly what
you do not want to do.
- if you create a new object for each related object you will end up
copying your customer / vendor too. Again, not desirable
- Gary's code decided whether to copy by reference (copy the
relationship) or deep copy (create a new object) based on whether the
related object was owned by the relating object. However there are
lots of cases where you want to copy the object even though it is not
owned (that has been my experience anyway).
- when do you stop? Usually copying a single object is not
sufficient, you need to copy a graph of several objects. If you
follow the relationships and copy the related objects (as in the PO
items), you have to stop at some point or you will end up duplicating
more than you intend.
Chuck
Jonathan Rochkind wrote:
At 03:35 PM 7/31/2003 -0700, you wrote:
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.
Our messages probably crossed paths, but if you wanted to point out the
problems with the trivial solution I posted to the list just now, it
would be educational.
--Jonathan
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.
--
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.