Name

Stored Procedures for Primary Keys Pattern

Goal

Efficiently assign primary keys to new entities, using the database’s key management techniques without performing multiple queries.

Participants

Data object

Tracks data within a system.

DAO

Handles persistence for objects within the system.

Stored procedure

Creates new rows in the database table to represent the data object.

Database table(s)

Stores the data associated with the data object.

Interactions

The client passes the data object to a DAO object to be used in the creation of a new entity in the database. The DAO passes this information to a stored procedure, which generates a new primary key and inserts the data into the target table, or inserts the data into the target table and allows the database to assign a primary key, which the stored procedure can then retrieve. The DAO then passes the new key back to the application.

Get J2EE Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.