The term “Entity Bean” is used loosely—sometimes it means the in-memory Java object, sometimes it means the database record. This confusion makes discussions about entity beans unclear. What’s the difference?
The EJB spec clarifies two distinct concepts:
- Entity Bean Instance: The in-memory Java object (an instance of your entity bean class). It’s the “view” into the database.
- Entity Bean Data: The actual persistent data stored in the database (the record/row).
The instance loads data from the database (in ejbLoad()), modifies it in memory, and saves it back (in ejbStore()).
Database Record (Entity Bean Data)
↓ ejbLoad()
In-Memory Object (Entity Bean Instance)
↓ business methods modify fields
In-Memory Object (with changes)
↓ ejbStore()
Database Record (updated)
- Instance is temporary: Created when needed, may be pooled/reused for different data
- Data is permanent: Survives server crashes, lives in database
- One-to-one mapping: At any moment, one instance represents one data record
- Container manages sync:
ejbLoad()andejbStore()keep instance and data in sync
graph semantic__Entity_Bean_Instance_vs_Data_ {
layout=neato
node [shape=ellipse fontname="Helvetica" fontsize=11 style=filled]
THIS [label=""Entity Bean Instanc" fillcolor="#ffd700" fontsize=13 style="filled,bold"]
REL1 [label="Related Concept" fillcolor="#f0f0f0"]
REL2 [label="Builds Into" fillcolor="#d4edda"]
THIS -- REL1 [label="related"]
THIS -- REL2 [label="builds into"]
}- Built from: Entity Bean, ejbLoad(), ejbStore()
- Builds into: BMP, CMP
- Related: Persistence Concepts
- Contrasts with: Session beans (no persistent data, only in-memory instances)
- Stale data: If another client modifies the DB directly, in-memory instance has old data (until next
ejbLoad()) - Instance pooling: The same Java instance might represent Ray’s account now, Bob’s account later (container reuses instances)
- Transparent to client: Client doesn’t know or care about instance vs data distinction—they just call methods