The Life Cycle of an Entity Bean

To understand how to best develop entity beans, it is important to understand how the container manages them. The EJB specification defines just about every major event in an entity bean’s life, from the time it is instantiated to the time it is garbage collected. This is called the life cycle, and it provides the bean developer and EJB vendors with all the information they need to develop beans and EJB servers that adhere to a consistent protocol. To understand the life cycle, we will follow an entity instance through several life-cycle events and describe how the container interacts with the entity bean during these events. Figure 11-2 illustrates the life cycle of an entity instance.

Entity bean life cycle

Figure 11-2. Entity bean life cycle

This section identifies the points at which the container calls each of the methods described in the EntityBean interface as well as the find methods and, in EJB 2.0, the select and home methods. Bean instances must implement the EntityBean interface, which means that invocations of the callback methods are invocations on the bean instance itself.

At each stage of the entity bean’s life cycle, the bean container provides varying levels of access. For example, the EntityContext.getPrimary() method will not work if it is invoked in the ejbCreate() method, but it does work when called in the ejbPostCreate() method. Other EJBContext

Get Enterprise JavaBeans, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.