Session beans don’t have built-in persistence like entity beans, but they still need to perform database operations involving relationships (joins). Without a pattern, developers might misuse session beans for persistence when entity beans would be better.
Session beans can perform persistence with relationships using JDBC (like BMP entity beans), but they lack identity and the container-managed lifecycle of entity beans. Stateful session beans can mimic entity beans but require manual coding; stateless session beans act as stateless persistence engines.
- Use like an entity bean — expose load/store methods that client calls
- Use JDBC to perform relationship queries (joins) manually
- All BMP entity bean relationship best practices apply (JNDI lookups, FK↔stub conversion)
- But: no container-managed identity, pooling behavior differs from entity beans
- No state held — can’t treat like an entity bean
- Acts as a service — reads/writes rows, marshals data back to client per method call
- Custom JDBC code for join queries must be written
- Cleaner for simple operations but tedious for complex relationships
- Stateless SB: no identity, acts as stateless persistence service
- Stateful SB: can mimic entity bean, but all JDBC code is manual
- Not recommended for complex relationships — entity beans (especially CMP) are better
- Session beans use JDBC directly, not CMR (like CMP entity beans do)
- Built from: Session Bean — session beans can do persistence with relationships
- Built from: JDBC — session beans use raw JDBC for relationship queries
- Contrasts with: Entity Bean — entity beans have built-in persistence; session beans don’t
- Contrasts with: Entity Bean Relationships — entity beans handle relationships automatically (CMP) or with less boilerplate (BMP)
- Related: BMP — session bean relationship code is analogous to BMP
- Complex relationships in session beans = lots of manual JDBC code (not recommended)
- Stateful SB holding state across method calls can impact scalability (uses passivation)
- Stateless SB must marshal all data back per method call (no state retained)
- Entity beans (CMP) are the preferred choice for complex relationships