When an entity bean instance is pooled and reused to represent different database records, how does the bean know which specific record it is currently handling during callbacks like ejbLoad() or ejbRemove()?
getPrimaryKey() is a method on EntityContext that returns the primary key of the entity currently associated with the bean instance. This lets a BMP bean identify the exact database row to load, store, or delete.
- Bean calls
ctx.getPrimaryKey()to get its current identity - Container sets the primary key in the context when assigning a data instance to the bean
- Bean must query this in
ejbLoad()to know which record to SELECT - Bean must query this in
ejbRemove()to know which record to DELETE - In
ejbStore(), the bean already has data in memory, so not needed
- Returns a Primary Key object (e.g., AccountPK) uniquely identifying the entity
- Required in BMP implementations; optional in CMP
- Called right after activation and before passivation
- Bean instance may switch data instances multiple times during lifecycle
- Built from: Entity Context, Entity Bean
- Builds into: ejbLoad(), ejbRemove(), Bean-Managed Persistence
- Related: Finder Methods, Primary Key
- Do NOT call in
ejbStore()—data is already in memory - Must be called on the EntityContext, not on
this - Pooled beans don’t have an identity until activated
- If called when context is null, throws IllegalStateException