J2EEOnline J2EE

Distributed Objects   «Prev  Next»
Lesson 7Entity beans
Objective What are Entity Beans?

What are Entity Beans?

An Entity bean[1] is associated with some persistent information somewhere.
The bean object actually has a one-to-one relationship with the information that is stored permanently.
One of the other major differences between session and entity beans is that entity bean instances are shared between multiple clients.
This is because multiple client programs may require access to the same data at the same time.
Entity beans use transactions to prevent concurrent access by multiple clients from corrupting the values in the data inadvertently.
Transactions are also required if other non-EJB programs are accessing the data simultaneously.

Entity bean
Entity bean

The characteristics of an entity bean are as follows:
  1. Provides an object view of transactional data in a database or to anything that has a persistent state.
  2. Allows shared access from multiple users. In the diagram above, client-A and client-B are sharing the same bean instance.
  3. Can be long lived, that is lives as long as the data it represents.
  4. Has a primary key that is used to identify it uniquely. Each entity bean instance has, through its EJBObject, a unique identity associated with the identity (primary key) of the entity object.
  5. Will survive the crash of the container.
    This is because the bean instance is only a representation of the actual persistent data and can be re-created from the data after a crash.
    The crash is almost transparent to the client (it may receive an exception). In fact, the container can discard arbitrarily any entity bean instance as it can always recreate it from the underlying data when required. Discarding the bean only discards the in-memory instance, not the underlying data. The container will usually discard the bean instance when it needs to free up resources (usually memory) and the instance has not recently been accessed.
  6. If the bean instance is removed, the underlying data is removed also.

A typical EJB container and server will provide a scalable runtime environment for a large number of concurrently active entity objects.
An example of an entity bean is a Customer, a BankAccount, or a Product that maps to rows in actual databases.

Entity Bean - Quiz

Click the Quiz link below to make sure you understand the different types of beans.
Entity Bean - Quiz
In the next lesson, the deployment descriptor and the jar file used to package a bean will be discussed.

[1] Entity bean: An EJB that is an object representation of a piece of persistent data.