Guideline: Designing Entity Beans
This guideline discusses how to design Entity Beans for a J2EE application.
Related Elements
Main Description


This guideline focuses on designing entity beans. Additional guidance on entity beans, such as how to identify and model them, is provided by Guideline: Entity Bean. General guidance on EJBs is provided by Guideline: Enterprise JavaBean (EJB)

Persistent Attributes and Primary Key

Identify the persistent attributes and the primary key of the entity bean.

Bean-managed Persistence (BMP) vs. Container-managed Persistence (CMP)

See Concept: Java 2 Platform Enterprise Edition (J2EE) Overview for a description of these two approaches to persistency.

In general, container-managed persistence requires less and simpler code than bean-managed persistence. However, if performance is critical or more control over the logical to database mapping is required, then you might need to use bean-managed persistence.

If your EJB container doesn't have the level of support that you require, you may wish to consider using a third-party, object-relational mapping tool, either with bean-managed persistence or, if the tool is integrated with your container, with container-managed persistence.

If you decide to use container-managed persistence and there is a a Data Model, you can map the CMP entity EJB's to database tables in the Data Model. This mapping will ultimately be reflected in the deployment descriptor of the EJB modules. For more information, see Guideline: Assembling J2EE Modules.

If you decide to use bean-managed persistence, encapsulate all database access within a Data Access Object (DAO) class. This is just a Java class that hides and encapsulates the underlying storage mechanism, and isolates changes when, and if, the interface to the data source changes. Even if the data source is expected to always be an RDBMS, there could be subtle differences in SQL syntax and format that will benefit from being isolated in a DAO class. See Core J2EE Patterns - Data Access Object Pattern ([ALU01].