Session beans are all equivalent (especially stateless)—there’s no way to distinguish one from another. But entity beans represent specific data records (like a specific bank account). How do you identify which entity bean instance represents which record?
Entity beans have an identity—a primary key that uniquely distinguishes each instance. This allows:
- Comparing two entity beans (“Are they the same account?”)
- Clients referring to specific entities by their primary key
- Sharing entities across multiple clients (unlike session beans which are client-dedicated)
The identity is typically the primary key of the database row the entity represents.
- Define primary key class:
AccountPKwith fields matching the table’s primary key getPrimaryKey()method: Entity beans implement this to return their primary key- Finder methods:
ejbFindByPrimaryKey(PK)locates the correct database record - Client reference: Client can pass primary keys to other clients, enabling shared access
- Primary key = identity: Two entity beans with same PK represent the same data
- Shared across clients: Unlike session beans, multiple clients can use the same entity
- Not just for databases: Identity concept applies even if using object databases
getPrimaryKey()in BMP: Bean uses PK to know which record to load/store inejbLoad()/ejbStore()
- Built from: Entity Bean, Primary Key Class
- Builds into: Finder Methods (locate entities by identity)
- Related: ejbCreate(), ejbLoad(), ejbStore()
- Contrasts with: Session Bean (no identity—anonymous workers)
- Composite primary keys: Sometimes PK has multiple fields—need a custom
PKclass - Identity crisis in pool: Pooled entity beans can represent different records at different times (container swaps the data)
getPrimaryKey()not for clients: It’s for BMP beans to know which record to access