• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

Overview

Entity beans represent persistent data in EJB. The key architectural decision is who handles the persistence: the container (CMP) or the bean itself (BMP).

Comparison

FeatureContainer-Managed Persistence (CMP)Bean-Managed Persistence (BMP)
Developer writes JDBC?NoYes
O/R MappingDefined at deployment timeHand-coded
PortabilityHigh (storage-independent)Lower (DB-specific code)
Code SizeSmaller beanMore code to maintain
ControlLess control over SQLFull control over SQL
ComplexitySimpler for developersMore error-prone

How It Works

CMP (Container-Managed Persistence)

  1. Developer writes entity bean with no persistence logic
  2. Using vendor tools, developer defines how fields map to database columns
  3. At deployment, container generates the SQL code
  4. Container handles all INSERT, UPDATE, DELETE, SELECT automatically

BMP (Bean-Managed Persistence)

  1. Developer writes JDBC code in ejbCreate(), ejbRemove(), finder methods
  2. Developer handles all SQL statements manually
  3. Container calls the methods but doesn’t generate SQL
  4. More flexibility but more responsibility

When to Use

Use CMP When:

  • Simple persistence needs
  • Portability across databases is important
  • Want to minimize code
  • Standard CRUD operations suffice

Use BMP When:

  • Need complex SQL queries
  • Require fine-grained control over database operations
  • Using legacy or unusual data sources
  • Specific database optimizations needed

Key Insight

CMP is the EJB “magic”—the container does the heavy lifting. BMP gives you the keys to the car. Choose based on your team’s expertise and the complexity of your data access needs.