Entity Beans   «Prev  Next»

Lesson 5Finding an entity bean using the primary key
ObjectiveWrite the code to lookup a bean using its primary key.

Locating Enity Bean

Finding the data

If the client knows the primary key of the persistent data that it needs, it can use that primary key to look up the bean.
The home object has a method findByPrimaryKey(PrimaryKey) that is used to create an instance of the bean and initialize it with the persistent data. The mechanism is described in the following Slide Show:


Finding Entity Bean Using Primary Key

Once the EJBObject and the bean instance are created, the client can access the methods of the instance to retrieve or change the data.

Subsequent client access

The bean instance for that data now exists. If any client subsequently invokes the findByPrimaryKey() method on the home object, passing it the existing primary key, then the container simply returns the remote reference to the EJBObject. There is no lookup in the database or creation of a new instance or EJBObject. It is very efficient. The following diagram illustrates what happens:

Subsequent client access Client and EJB Objects

Finding Entity Bean - Quiz

Take a moment to test your knowledge on finding an entity bean using the primary key by clicking the Quiz link below.
Finding Entity Bean - Quiz
The next lesson introduces Bean-Managed Persistence (BMP).