EJB Types
EJBs are distinguished along three main functional roles. Within each primary role, the EJBs are further distinguished according to subroles. By partitioning EJBs into roles, the programmer can develop an EJB according to a more focused programming model than if such roles were not distinguished. Such roles also allow the EJB container to determine how to best manage a particular EJB based on its programming model type.
These are the three main distinctions of EJBs:
Session beansSession beans are EJBs that are created to perform some action on the enterprise system and possibly return results to the client. Session beans correspond to the controllers of a system exposed for manipulation to clients.
Entity BeansEntity beans are EJBs that are created to encapsulate some data contained by the enterprise system. Such data may be created, removed, or found by clients. Data also may be retrieved and updated by clients with EJB containers that determine how any updates occur based on the transaction semantics for a particular entity bean. Entity beans also have special primary key classes defined for them that relate to the primary keys of an associated entity stored in the database. Entity beans correspond to some enterprise system entity.
Message-driven beansMessage-driven beans are EJBs (new to the EJB v2.0 specification) whose methods may be invoked asynchronously. Thus, unlike the synchronous method invocation nature of pure session and entity beans, message-driven beans are registered with a container and invoked by the container. EJB clients send messages to a queue or topic offered up by the container, and the container determines when to invoke the message-driven bean with the particular message sent by the client.
Within the realm of session beans, two further distinctions may be made:
Stateless session beansStateless session beans represent session EJBs created with no regard for the maintenance of any state between subsequent calls by a client. Stateless session beans thus represent pure input and output engines.
Stateful session beansStateful session beans represent session EJBs created to maintain state for a particular client between subsequent calls before some maximum amount of time has expired. Stateful session beans thus represent input and output engines that can utilize the state created by a client from a previous invocation.
Within the realm of entity beans, two distinctions also apply:
Bean-managed persistence (BMP) entity beansBMP entity beans represent entity EJBs in which all code dealing with the insertion, deletion, querying, and update of data to a relational data source (such as a database) is performed by the entity EJB developer.
Container-managed persistence (CMP) entity beansCMP entity beans represent entity EJBs in which all code dealing with the insertion, deletion, querying, and update of data to a relational data source (such as a database) is provided by the container EJB implementations. Containers primarily provide such an implementation by using deployment descriptor-based information to map EJB class fields to relational data table columns and then subsequently generate the SQL code using these mappings.