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.
Using a Delegate
Using Binary Keys
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.
Enterprise Objects provides another useful mechanism to generate primary keys. It generates a binary primary key if a primary key meets these criteria:
Its external data type is a “raw bytes” data type, such as Oracle RAW and OpenBase binary.
Its internal data type is NSData.
Its internal data type width is 24 bytes.
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:
It doesn’t require a round trip to the database.
It is not database dependent.
The generated keys are globally unique.
Binary primary keys have the following characteristics, which can be considered disadvantages:
The generated keys are quite large.
Comparing keys requires more computing resources.
Binary keys can’t be part of a compound primary key.
Last updated: 2007-07-11