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.1 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.

You are also able create EJB 3.0 and 3.1 beans in Web 3.0 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.

Guidelines for developing EJBs

While EJB 3.1 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:


Feedback