Re: Setting an attribute based on PK?

Mark Ritchie <[email protected]> Thu, 13 May 2004 07:19:20 -0400
Newsgroups gmane.comp.web.webobjects.eof
Message-ID <[email protected]>
On 25-Apr-04, at 9:07 AM, Ashley Aitken wrote:
> Just saw some code on WODev (thanks to all who have created that 
> wonderful resource) for generating primary keys on insert.  I will try 
> that and see how it goes.
>
> Of  course, the downside is that the primary keys may not get used (ie 
> no saveChanges) and you lose any sequencing/counting on the primary 
> key (but then it is supposed to be database-specific not 
> app-specific).

Hi Ashley,

One approach might be to initiate some custom code just before you call 
saveChanges.
That code would:
1) initiate PK generation manually and assign the PK
2) determine if value(s) need to be derived from the PK and set the 
value(s)
3) initiate the saveChanges

This approach has the benefits:
a) changes to the EO are all made prior to the call to saveChanges  
(what EOF expects.)
b) there's no need to guess when the PK generation occurs during the 
saveChanges
c) if you hold off generating the PK until the user has initiated the 
save then you have
much less chance of the PK not getting used.  This avoids gaps in the 
PK's due to the
user deciding to not save an EO which has been partially created.

Note: It's perfectly legal to set the PK manually prior to saveChanges. 
EOF will use the
value which you've assigned and it's easy for you if you ask EOF to 
give you the next
available PK for that entity. (see EOAdaptorChannel - 
primaryKeysForNewRowsWithEntity)

Good luck!
Mark.
__
Mark Ritchie, [email protected] 416-843-5479
Software Developer, Apple Certified Developer Trainer
Diamond Lake Consulting Inc., Toronto, Ontario, Canada