Re: One-to-One Relationship
Re: One-to-One Relationship
- Subject: Re: One-to-One Relationship
- From: Christian Brunschen <email@hidden>
- Date: Wed, 22 Mar 2006 17:29:13 +0000
On 22 Mar 2006, at 16:50, Christian Pekeler wrote:
I have two objects setup tied by a bidirectional one-to-one
optional relationships.
Problem:
When I use "addObjectToBothSidesOfRelationshipWithKey"
only half of the relationships get updated. I crafted the
following code that does update both sides
I don't undestand why "addObjectToBothSidesOfRelationshipWithKey"
doesnt' work and suspect I screwed up somewhere.
EOCustomObject.addObjectToBothSidesOfRelationshipWithKey(obj, key)
is only taking care of both sides if it can find the reciprocal
(also called inverse or back-) relationship to the relationship
you've specified as key. To do that,
addObjectToBothSidesOfRelationshipWithKey is using
EORelationship.inverseRelationship() which searches the destination
entity for a relationship that points back to our entity using a
reciprocal join.
For example, if we have a relationship from A to B using the join
[A.a_id, B.a_id] (in other words, B has a foreign key a_id for a to-
many relationship from A to B),
addObjectToBothSidesOfRelationshipWithKey is looking for a back-
relationship with the join [B.a_id, A.a_id] (in other words, we're
looking for a to-one relationship from B to A using the same keys/
attributes/columns).
In your case with the optional one-to-one relationship, the joins
are [A.b_id, B.b_id] and [B.a_id, A.a_id] which are not reciprocal,
i.e. EORelationship.inverseRelationship() can't identify these
joins as being related to each other. You didn't "screw up" - this
is just a case where you need to explicitly maintain both sides of
the relationship. One way to do that would be to overwrite
addObjectToBothSidesOfRelationshipWithKey in A and B to check if
the key is for a one-to-one relationship and then specifically deal
with it.
A potentially easier way would be to override the entity class's
'inverseForRelationshipKey' method - to quote from the documentation
on EOEnterpriseObject, at <http://developer.apple.com/documentation/
WebObjects/Reference/API/com/webobjects/eocontrol/
EOEnterpriseObject.html#inverseForRelationshipKey(java.lang.String)> :
<quote>
public String inverseForRelationshipKey(String relationshipKey)
Returns the name of the relationship pointing back to the receiver's
class or entity from that named by relationshipKey, or null if there
isn't one. With the access layer's EOEntity and EORelationship
classes, for example, reciprocality is determined by the join
attributes of the two EORelationships. EOCustomObject's
implementation simply sends an inverseForRelationshipKey message to
the receiver's EOClassDescription.
You might override this method for reciprocal relationships that
aren't defined using the same join attributes. For example, if a
Member object has a relationship to CreditCard based on the card
number, but a CreditCard has a relationship to Member based on the
Member's primary key, both classes need to override this method. This
is how Member might implement it.
public String inverseForRelationshipKey(String relationshipKey){
if (relationshipKey.equals("creditCard"))
return( "member" );
else
return( super.inverseForRelationshipKey(relationshipKey) );
}
</quote>
It should be straightforward to use this to return the inverse to-one
relationship in each of the affected entity classes.
Christian
// Christian
_______________________________________________
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