Lesson 6 | Bean-managed persistence |
Objective | Describe bean-managed persistence. |
Bean-Managed Persistence
There are two types of persistence for entity beans:
- (BMP) bean-managed persistence
- (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:
ejbLoad()
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:
- When the initial
findByPrimaryKey()
is invoked within a container for a specific primary key.
- Before the bean is pooled (see later)
- After a transaction begins
- 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:
- How to make a JDBC connection
- The location of the persistent store
- 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