How does an entity bean communicate with the EJB container to access information about its environment, identity, and the transaction it is participating in?
Entity Context is an interface that provides the entity bean instance with access to container services and information about the current entity it represents, including the primary key that identifies the database record.
- Container sets the EntityContext on the bean via
setEntityContext() - Bean can query the context for security info, transaction status
- Critical method:
getPrimaryKey()returns the primary key of the current entity - Required for BMP beans to know which database record they represent
- Context is set when bean is associated with an EJB object
- Interface:
javax.ejb.EntityContext - Provides
getEJBObject()andgetEJBLocalObject()for getting the bean’s proxy - Provides
getPrimaryKey()to identify which data instance the bean represents - Bean instance is pooled and may represent different data at different times
- Built from: Entity Bean, EJB Container
- Builds into: getPrimaryKey(), ejbLoad(), ejbRemove()
- Related: Session Context for session beans
- Pooled instances don’t have a context until associated with an EJB object
- Calling
getPrimaryKey()beforesetEntityContext()will fail - Context is null when bean is in the pool