How does an entity bean synchronize its in-memory state with the database when the container loads data for a specific entity? How does the bean know which database row to load?
ejbLoad() is a container callback that loads entity state from the database into the bean instance. In BMP, the bean must call getPrimaryKey() to determine which record to load, then execute a SELECT query.
- Container calls
ejbLoad()on the bean instance - Bean calls
ctx.getPrimaryKey()to get the current entity’s ID - Bean acquires JDBC connection
- Bean executes
SELECTquery using the primary key - Bean populates its fields from the ResultSet
- Called when container assigns a data instance to a bean
- Called after
ejbActivate()or at the start of a transaction - In BMP: developer writes the SELECT logic
- In CMP: container auto-generates the SELECT
- Must call
getPrimaryKey()to know which data to load
- Built from: Entity Bean, getPrimaryKey(), Entity Context
- Builds into: ejbStore() (opposite operation)
- Related: JDBC, ejbActivate()
- Not called on every method—only when loading from DB is needed
- Called BEFORE business methods in the ready state
- Don’t confuse with
ejbActivate()which acquires resources, not data