Creating enterprise beans
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.
The EJB 3.0 specification deprecates EJB 1.1-style entity beans. The Java Persistence API (JPA) specification is intended to replace the deprecated enterprise beans. While the JPA replacement is called an entity class, it should not be confused with entity enterprise beans. A JPA entity is not an enterprise bean and is not required to run in an EJB container.
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 or @Stateless indicates that the class is a session bean class; 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.0 specification requires a bean class and a business interface.
- 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.
- 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.
- 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 arraives. 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 make use of 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.0 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 will add a default constructor
- The POJO must implement at least one POJI (plain old Java interface); you need at least one 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.
Related tasks
Creating enterprise beans using wizards
Creating enterprise beans using annotations
Importing JAR files to EJB 3.0 projects