Entity Beans   «Prev  Next»
122
Lesson 6Bean-managed persistence
Objective Describe bean-managed persistence.

Bean-Managed Persistence

There are two types of persistence for entity beans:
  1. (BMP) bean-managed persistence
  2. (CMP) container managed persistence
In bean-managed persistence, the bean is totally responsible for the loading and storing of the data that it represents.
In container-managed persistence, the responsibility for loading and storing the data is delegated to the container.

The bean's Responsibilities

The entity bean must be able to look up, load, and store the data that it maps to.
The container still controls the show, telling the bean to load and store the data through bean callbacks.
The callbacks are:
  1. ejbLoad()
  2. ejbStore()

By the way, are you familiar with the callback methods?
Go to page 30 and 31 of Enterprise JavaBeans by Richard Monson-Haefel.
We will cover these in detail when we write the code for the bean. These methods use SQL, CORBA, or whatever technique is suitable for accessing the persistent entity from the EJB platform.

When are these Callback Methods called?

The callback methods are called whenever the container needs the bean to synchronize itself with the underlying data.
Some of examples of this are:
  1. When the initial findByPrimaryKey() is invoked within a container for a specific primary key.
  2. Before the bean is pooled (see later)
  3. After a transaction begins
  4. Before a transaction is committed

Problems with bean-managed persistence

The bean needs to know, or have access to, the following information to be able to access the data:
  1. How to make a JDBC connection
  2. The location of the persistent store
  3. The schema of the data
Bean-managed entity beans are less portable than container-managed entity beans because their code contains very specialized information. That information may change on the fly. Some of these problems can be minimized by storing relevant information in JNDI and not in the bean.
The next lesson introduces container-managed persistence.

Java EE 8 Application Development