• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: One-to-One Relationship
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: One-to-One Relationship


  • Subject: Re: One-to-One Relationship
  • From: wojingo <email@hidden>
  • Date: Wed, 22 Mar 2006 18:04:58 +1030

Louis Demers wrote:
Hi,
-----------
Background:

I have two objects setup tied by a bidirectional one-to-one optional relationships. both classes have acessors for the relationships in their classe.

TelehoneLine.java contains
public class TelephoneLine extends EOGenericRecord {
...
    public Contact employee() {
        return (Contact)storedValueForKey("employee");
    }

    public void setEmployee(Contact value) {
        takeStoredValueForKey(value, "employee");
    }
...
}
Contact.java contains
public class Contact extends EOGenericRecord {
...
    public TelephoneLine telephoneLine() {
        return (TelephoneLine)storedValueForKey("telephoneLine");
    }

    public void setTelephoneLine(TelephoneLine value) {
        takeStoredValueForKey(value, "telephoneLine");
    }
...
}

Both one-to-one relationships (employee and telephoneLine) are defined
optional, nullify, DO NOT own destinations. and their foreign keys allow zeros.


--------
Problem:

When I use "addObjectToBothSidesOfRelationshipWithKey"

aTelephoneLine.addObjectToBothSidesOfRelationshipWithKey(selectedEmployee,"employee");


only half of the relationships get updated. I crafted the following code that does update both sides


-----------
Workaround:

    if (aTelephoneLine.employee() != null ){
        aTelephoneLine.employee().setTelephoneLine(null);
    }
    aTelephoneLine.setEmployee(selectedEmployee);
    if (selectedEmployee != null ){
        selectedEmployee.setTelephoneLine(aTelephoneLine);
    }

but I don't undestand why "addObjectToBothSidesOfRelationshipWithKey" doesnt' work and suspect I screwed up somewhere.



It sounds like you have two, to-one relationships, if so, each will need to be set as you have done above or using addobjects..


Or as Robert said previously you can create a to-many and only ever add one object on the many side, writing the relevant helper methods to return the single object or add/replace for that matter. Then you only need one call to addobjects.. This might be a good approach as its flexible for the day when someone has to share a phone line. :)

cheers,
- shaun
















_______________________________________________ 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
References: 
 >One-to-One Relationship (From: Louis Demers <email@hidden>)

  • Prev by Date: Re: One-to-One Relationship
  • Next by Date: Re: Webobjects-dev Digest, Vol 3, Issue 187
  • Previous by thread: Re: One-to-One Relationship
  • Next by thread: Re: One-to-One Relationship
  • Index(es):
    • Date
    • Thread