Documentation Archive Developer
Search
PATH  Documentation > WebObjects 4.5 > Programming Topics

  

Generating Primary Key Values

Synopsis

Describes how the primary key is generated for a new object in EOF. It also explains the various ways the user can generate a primary key.

Description

Primary keys are used in a database to identify a row in a table. Each key is generated on the client side before its corresponding row is inserted into the database.

There are several ways to generate the primary key, some of which are outlined below.

Class Property of an Enterprise Object

The primary key can be generated by the user in the Enterprise Object (EO) when it is created. EOModeler allows the primary key to be made a class property and hence, an instance variable of the Enterprise Object. The user has the responsibility of creating a unique primary key each time a new EO is instantiated.

Adaptor-Generated Primary Key

In Enterprise Objects Framework's default primary key mechanism, the EOAdaptorChannel generates the primary keys. It collaborates with the database server to maintain a linear sequence of nonrepeating primary keys per entity. The primary key attribute must not be a class property. If the type of the primary key attribute is a 12-byte NSData, EOF generates unique primary keys without going to the database.

Advantages

  • Easy-to-use; the user does not have to bother with primary key generation.

Disadvantages

  • Server-specific.
  • Requires server setup using EOModeler's SQL generation.
  • Requires a round-trip to the database at run time for each new key.
  • Cannot generate a compound primary key.

Delegate of the Database Context

Before using the adaptor's default primary key generation, the EODatabaseContext's delegate is given a chance to generate the primary key. This method is used only if it satisfies the following conditions

  • The database context has a delegate.
  • The delegate implements databaseContextNewPrimaryKeyForObject .
  • The method returns a non-null value.

Custom Stored Procedure

The user can create a stored procedure for the generation of the primary keys and add it to model. The stored procedure is set as the primary key stored procedure for the entity.

Advantages

  • Useful if non-EOF applications share the database.

Disadvantages

  • Requires a round trip to the server for each primary key.

Propagation of Primary Keys

The EOModeler allows the user to set the "Propagate Primary Key" in the advanced relationship inspector. When the user creates an object within an entity, that entity's primary key is used as the primary key for the new object.

For example, a column in table A can be the primary key for table A, as well as the foreign key to a table B.

See Also

Questions

Keywords

Revision History

22 July, 1998. Seejo Pylappan. First Draft.
19 November, 1998. Clif Liu. Second Draft.

 

© 1999 Apple Computer, Inc.