How do you avoid writing repetitive JDBC/database code for every entity bean?
With container-managed persistence (CMP), the EJB container automatically handles all database operations. The developer writes no JDBC code—the container generates SQL statements for create, read, update, and delete operations.
- Developer strips entity bean of any persistence logic
- Developer uses vendor tools to define O/R mappings (how bean fields map to database columns)
- At deployment time, container generates the data access code
- Container automatically performs INSERT, UPDATE, DELETE, and SELECT operations
- Mapping can be done at deployment time, making beans storage-independent
- Container handles all persistence automatically
- No JDBC code in the bean
- O/R mapping defined at deployment time
- Storage-independent—can switch databases without changing bean code
- Reduces bean size significantly
- Built from: Entity Bean, EJB Container
- Builds into: ejbCreate(), ejbRemove(), ejbLoad(), ejbStore(), CMP Abstract Accessors, CMP Lifecycle
- Contrasts with: Bean-Managed Persistence
- Related: Object-Relational Mapping, EJB-QL, CDATA Hack, One-to-One Relationship, One-to-Many Relationship, Many-to-Many Relationship, Bidirectional vs Unidirectional
- Less control over exact SQL generated
- Vendor-specific tools may be required for mapping
- May not support all database features