Container Managed Persistence (CMP) versus Bean Managed Persistence (BMP) Entity Beans
The past couple of articles discussed the two types of Enterprise JavaBeans defined in the EJB 1.1 specification: entity beans and session beans. In this article, we will focus our attention on entity beans, but this time we will consider the differences between the two types of entity beans: container-managed and bean-managed. We will discuss the benefits and drawbacks of using BMP, as well as the design decisions to consider when making the choice between BMP and CMP. (Also, note that this is one of my favorite interview questions, so pay attention.)
If you are looking forward to some of the new EJB 2.0 features, just sit tight—we will address those in forthcoming articles. And do not fret—the material we will be covering will be applicable to your daily programming lives!
Entity Beans
Recall that entity beans represent data; in our design paradigm, we refer to entity beans as the "nouns" or "things" in our system. Session beans represent business logic that manipulates our data; these are the "verbs" in our system. Together, these two types of beans provide a logical breakdown of our problem into their respective roles.
Entity beans come in two flavors, based on their persistence management: container-managed and bean-managed. The lifecycle of container-managed beans is controlled by the EJB container; it is responsible for persisting the bean's fields to and from the database. The lifecycle of bean-managed beans is triggered by the EJB container through notifications to the bean's implementation class, but the actual persistence to and from the database is left to the bean itself.