Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Generating Custom Primary Keys

There are many reasons why you may want to or need to generate custom primary keys in an Enterprise Objects application. They are discussed in “Key Generation.” The following sections provide sample implementations of two of the custom-key generation mechanisms, using the EODatabaseContext delegate and using the automatic key generation for binary primary keys.

In this section:

Using a Delegate
Using Binary Keys


Using a Delegate

The EODatabaseContext class provides a delegate in which you can generate custom primary keys. This is especially useful if an entity has a compound primary key, but it is also useful if your application can’t use Enterprise Object’s automatic key generation for simple integer primary keys.

You can set the delegate by invoking EODatabaseContext.setDefaultDelegate(this) in the class in which you implement the delegate method. An implementation of databaseContextNewPrimaryKey is shown in “Listing 8-1.”

Listing 7-1  databaseContextNewPrimaryKey implementation

public NSDictionary databaseContextNewPrimaryKey(EODatabaseContext dbCtxt, Object object, EOEntity entity) {
        NSArray rawRows = EOUtilities.rawRowsForSQL(new EOEditingContext(), "PKTester",          "SELECT MAX(FOO_PK) FROM FOO");
        NSDictionary rowWithPK = (NSDictionary)rawRows.objectAtIndex(0);
        Object maxPK = rowWithPK.objectForKey("FOO_PK");
        int pk = (new Integer(maxPK.toString())).intValue();
 
        NSMutableDictionary newPrimaryKey = new NSMutableDictionary();
        NSArray entityPrimaryKeys = entity.primaryKeyAttributeNames();
 
        Enumeration primaryKeyEnumerator = entityPrimaryKeys.objectEnumerator();
        while (primaryKeyEnumerator.hasMoreElements()) {
            String pkName = (String)primaryKeyEnumerator.nextElement();
            newPrimaryKey.takeValueForKey(new Integer(++pk), pkName);
        }
        return newPrimaryKey;
 }

The method in “Listing 8-1” returns a dictionary of key-value pairs; the keys are the names of the entity’s primary key attributes (which are returned by the method EOEntity.primaryKeyAttributeNames) and the values are the values you generate in the method for each of those attributes.

In “Listing 8-1,” a SQL expression is sent to the database to determine the highest value for the entity’s primary key, FOO_PK. That value is stored in the pk variable, which is incremented in the while loop to generate a unique primary key.

Using Binary Keys

Enterprise Objects provides another useful mechanism to generate primary keys. It generates a binary primary key if a primary key meets these criteria:

The binary primary key generated in this case is a globally unique key based on an enterprise object’s EOTemporaryGlobalID. Generating primary keys this way has these advantages:

Binary primary keys have the following characteristics, which can be considered disadvantages:



< Previous PageNext Page > Hide TOC


Last updated: 2007-07-11




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice