Re: A better way so save datasets with relations?

Helmut Schottmüller <[email protected]>
Newsgroups gmane.comp.web.webobjects.devel
Message-ID <[email protected]>
Hi Jerry,

thanks a lot! WO is so easy that I need some time to deal with it :-)
I never thought of the possibility to make the foreign key in the  
table NOT a class attribute.
Thanks for the advice with the naming conventions. I renamed the  
classes.

Best regards,

Helmut

Am 04.01.2007 um 19:06 schrieb Jerry W. Walker:

> Hi, Helmut,
>
> First, I would suggest renaming your classes to Category and  
> Keyword, that is, without the "EO" prefix which should be reserved  
> for classes defined in the EOFrameworks.
>
> Pay careful attention to the first four columns in EOModeler's  
> table mode when an entity is selected for editing. If we label the  
> four columns as follows: <key>, <diamond>, <lock>, and <0>, I would  
> suggest defining your two entities as follows:
>
>                    <key>  <diamond> <lock> <0>
> Category
>     id                
> X                                               (primary key)
>     category                     X            X                  
> (varchar)
>
> Keyword
>     id                
> X                                               (primary key)
>     keyword                     X            X                  
> (varchar)
>     fiCategory                                 X                  
> (id, foreign index of the category)
>     category                     X            X                  
> (relation, which defines an 1:1 relation between  
> EOKeyword.fiCategory and EOCategory.id)
>
> Be sure that the category attribute of Keyword is defined as a  
> relation, that is, when EOModeler is in Table mode, it is displayed  
> under the lower "Keyword Relationships" section of the Table Mode  
> view rather than the upper "Keyword Attributes" section.
>
> If you've defined your two entities as above, then, When you want  
> to save a new Keyword, your code should look as follows:
>
>      EOEditingContext ec = session().defaultEditingContext(); //  
> retrieve the EC
>      Category categoryObject; // Assume we already have one of  
> these and that it was retrieved, or created, in the ec  
> EOEditingContext
>
>      Keyword newKeyword = (Keyword) 
> EOUtilities.createAndInsertInstance(ec, "Keyword"); // create and  
> insert a new instance of Keyword
>      newKeyword.takeValueForKey(newkeyword, "keyword"); // set the  
> keyword with a value from a web form
>      newKeyword.addObjectToBothSidesOfRelationshipWithKey 
> (categoryObject, "category");
>
> In particular, note that the primary keys for both EOs are NOT  
> class properties, and therefore, are not visible in your Java code.  
> Let EOF take care of the keys for you. It tends to be very good at  
> that and often does less well if you're interfering with it's  
> efforts.  :-)
>
> If the code, as I've presented it, raises an exception, please copy  
> the stack trace when you ask about it. Presuming that you've got a  
> legitimate categoryObject at the start of the code, that is, one  
> that was retrieved or created in ec, then it should just work.
>
> Regards,
> Jerry
>
>
> On Jan 4, 2007, at 12:31 PM, Helmut Schottmüller wrote:
>
>> 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
>
>
> --
> __ Jerry W. Walker,
>    WebObjects Developer/Instructor for High Performance Industrial  
> Strength Internet Enabled Systems
>
>     [email protected]
>     203 278-4085        office
>
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.