Enterprise beans overview

After you have created your Java™ or EJB project, you can create session beans, entity beans, and message-driven beans to add to your project.

Enterprise beans

An enterprise bean is a Java component that can be combined with other resources to create Java applications. There are three types of enterprise beans: entity beans, session beans, and message-driven beans. All beans reside in Enterprise Java beans (EJB) containers, which provide an interface between the beans and the application server on which they reside.

You are also able create EJB 3.x beans in Web 3.x projects.

Component-defining annotations

Using component-defining annotations, you can create the following types of enterprise beans: session beans, message-driven beans, and JPA entities. Including the component-defining annotation @Stateful, @Stateless indicates that the class is a session bean class; including the component-defining annotation @Singleton indicates that the class is a singleton class; and including the component-defining annotation @MessageDriven indicates that the class is a Message-driven bean class; and including the component-defining annotation and @Entity indicates that the class is a JPA entity.

  • Session beans: At a minimum, a session bean developed with the EJB 3.x specification requires a bean class.
    1. Stateful: A stateful session bean maintains client-specific session information, or conversational state, across multiple method calls and transactions. An instance of a stateful session bean has a unique identity that is assigned by the container at create time.
    2. Stateless: A stateless session bean does not maintain conversational state. Instances of a stateless session bean have no conversational state. Because a stateless session EJB does not maintain a conversational state, all the data exchanged between the client and the EJB must be passed either as input parameters, or as return value, declared on the EJB business method interface. All instances of a stateless session bean have the same object identifier, which is assigned by the container.
    3. Singleton: Singleton session beans, new in EJB 3.1, is a new kind of session bean that is guaranteed to be instantiated once for an application in a particular Java Virtual Machine (JVM). Singletons offer similar functionality to stateless session beans but differ from them in that there is only one singleton session bean per application, as opposed to a pool of stateless session beans, any of which may respond to a client request. Like stateless session beans, singleton session beans can implement web service endpoints. Singleton session beans maintain their state between client invocations but are not required to maintain their state across server crashes or shutdowns.
  • Message-driven beans: Message-driven beans were introduced in EJB 2.0 to support the processing of asynchronous messages from a Java Message Service (JMS). The EJB 2.1 specification expands the definition of the message-driven bean so that it can support any messaging system, not just JMS. In simplest terms, a message-driven bean is a message consumer that can be called by its container. They are invoked by the container when a message arrives. Message beans are another interaction mechanism for invoking EJBs, but unlike session beans, the container is responsible for invoking them when a message is received, not a client (or another bean).
  • Entities using Java Persistence API (JPA): Entities use the new Java Persistence API that is part of the Java EE 5 platform. Unlike EJB components that use container-managed persistence (CMP), entity objects that use the new APIs are no longer components, but are simply Java objects. This makes entities more lightweight and the programming model simpler to use. For more information about JPA, see JPA documentation.

Guidelines for developing EJBs

While EJB 3.x provides a flexible and simple programming model, here are a few of the suggested rules for developing EJBs:
  • Each entity must be a POJO and the class must be concrete (therefore neither abstract or final).
  • The class must have a no-argument constructor; if none is present, the compiler adds a default constructor
  • The POJO must implement at least one POJI (plain old Java interface); you do not need to include an interface, but you can include different interfaces for local and remote clients.
  • If the business interface includes an @Remote annotation, all the parameters declared on the interface must implement java.io.Serializable.
  • A session EJB can subclass a POJO, but cannot subclass another session EJB.

You can create enterprise beans in one of the following ways:

  • Create new enterprise beans using wizards.
  • Create new enterprise beans using Java EE annotations.
  • Import enterprise beans from EJB JAR files.
Icon that indicates the type of topic Concept topic
Timestamp icon Last updated: July 17, 2017 21:58

File name: centerprisebeans.html