A better way so save datasets with relations?
Helmut Schottmüller <[email protected]>
| Newsgroups | gmane.comp.web.webobjects.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi Group,
again a rookie question :-( Sorry for that.
I have created two entities in my application:
EOCategory (which stores a category) and
EOKeyword (which stores a keyword of a given category)
EOCategory {
id (primary key)
category (varchar)
}
EOKeyword {
id (primary key)
keyword (varchar)
fiCategory (id, foreign index of the category)
category (relation, which defines an 1:1 relation between
EOKeyword.fiCategory and EOCategory.id)
}
so far I think this is the correct way to define a relation in
EOModeler.
When I want to save a new Keyword I have to do the following:
EOEditingContext ec = session().defaultEditingContext(); // retrieve
the EC
EOKeyword newKeyword = (EOKeyword)EOUtilities.createAndInsertInstance
(ec, "EOKeyword"); // create and insert a new instance of EOKeyword
newKeyword.takeValueForKey(newkeyword, "keyword"); // set the keyword
with a value from a web form
NSDictionary result = EOUtilities.primaryKeyForObject(ec,
categoryObject); // retreive the primary key of the related category
object
int pkey = ((Integer)result.objectForKey("id")).intValue(); //
retreive the primary key of the related category object
newKeyword.takeValueForKey(new Integer(pkey), "fiCategory"); // set
the foreign index in the EOKeyword instance
newKeyword.addObjectToBothSidesOfRelationshipWithKey(categoryObject,
"category"); // add the relation to the EOKeyword instance
In the beginning I thought that it should be sufficient to set the
keyword and to use addObjectToBothSidesOfRelationshipWithKey to set
the relation because I would expect, that
addObjectToBothSidesOfRelationshipWithKey takes care of the foreign
index attribute of the entity. But this didn't work. I always got an
exception which occured during the validation of the foreign index.
Is this the right way to save a dataset with a relation to the
database? I doubt that.
Could someone give me a clue what I can do better?
Thanks in advance,
Helmut
_______________________________________________
WebObjects-dev mailing list
[email protected]
http://www.omnigroup.com/mailman/listinfo/webobjects-dev
smime.p7s
(application/pkcs7-signature, 2.4 KB) - not displayed