Re: Adding to relationships - what method to use?
Re: Adding to relationships - what method to use?
- Subject: Re: Adding to relationships - what method to use?
- From: Robert Walker <email@hidden>
- Date: Wed, 19 Oct 2005 09:59:49 -0400
Following is an excerpt from the EnterpriseObject Programming Guide:
You should use these methods to manipulate your relationships when
you want to manage both sides. These methods manage one-to-one, one-
to-many, and many-to-many (flattened) relationships.
IMPORTANT NOTE: When you have a relationship that is set to propagate
it's primary key, but sure to use these methods on the "to-many"
side. Otherwise the primary key is not propagated properly and
exceptions are raised. The most common case for this is in many-to-
many relationships with exposed attributes (no flattened relationship).
------------------------------------------------------------------------
------------------------------------------------------
Manipulating Relationships
Enterprise Objects makes working with relationships rather simple.
All you need is two enterprise objects in the same editing context to
manipulate relationships programmatically. Were you to perform the
same kind of task in other database development environments, you’d
likely have to write many lines of SQL to relate a record in one
table with a record in another table. With Enterprise Objects,
however, a single method invocation does this for you.
Say you have an enterprise object representing a Document entity and
a relationship in that entity called writers, which represents the
authors of the document. To associate a new Writer record with the
Document record, you simply create an enterprise object for the
writer and then add it to the relationship with this code:
document.addObjectToBothSidesOfRelationshipWithKey(writer, "writers");
The first argument in the method invocation represents the Writer
object. The second argument corresponds to the name of the
relationship in the Document entity. If the relationship (in this
case writers) is modeled with an inverse relationship, this method
also adds the object to the other side of the relationship.
In addition to adding records in a relationship, you’ll likely also
need to remove them. Fortunately, Enterprise Objects provides another
method that does all the work for you:
document.removeObjectFromBothSidesOfRelationshipWithKey(writer,
"writers");
------------------------------------------------------------------------
------------------------------------------------------
Following is an excerpt from the EOCustomObject API that explains the
use of includeObjectIntoPropertyWithKey and
excludeObjectFromPropertyWithKey:
These methods do NOT manage both sides of the relationship. These
methods are used by addObjectToBothSidesOfRelationshipWithKey and
removeObjectFromBothSidesOfRelationshipWithKey to manage the
relationships (maybe not directly, but it has the same effect).
------------------------------------------------------------------------
------------------------------------------------------
includeObjectIntoPropertyWithKey
protected void includeObjectIntoPropertyWithKey(Object eo,
String key)
Adds to the relationship named key the object eo without affecting
inverse relationships, by directly modifying the property storing the
to-many relationship. This method can be used from inside of methods
of the form addToKey.
Parameters:
eo - the object to add to the relationship
key - the name of the relationship
---
excludeObjectFromPropertyWithKey
protected void excludeObjectFromPropertyWithKey(Object eo,
String key)
Removes the object eo from the to-many relationship named key without
affecting inverse relationships, by directly modifying the property
storing the to-many relationship. This method can be used from inside
of methods of the form removeFromKey. The method throws an
IllegalArgumentException when attempting to remove an object not
contained in the relationship array.
Parameters:
eo - the object to remove from the relationship.
key - the name of the relationship
Throws:
IllegalArgumentException - if eo is not in relationship key
------------------------------------------------------------------------
------------------------------------------------------
On Oct 19, 2005, at 2:13 AM, Greg Hulands wrote:
Hi,
When adding to or removing from a to-Many relationship, what method
is the best practice?
excludeObjectFromPropertyWithKey and includeObjectIntoPropertyWithKey
or
addObjectToBothSidesOfRelationshipWithKey and
removeObjectFromBothSidesOfRelationshipWithKey.
With the templates included with EOGenerator, it uses the first
one, but with other peoples templates they use the second one.
I have run into an issue with my code so I want to use the correct
way.
Regards,
Greg
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40mac.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden