Show TOC

Creating Session Beans in the Developer StudioLocate this document in the navigation structure

Prerequisites

An EJB project exists.

Context

You create session beans in the SAP NetWeaver Developer Studio using the EJB Session Bean 3.x wizard. With this wizard, you can create pure EJB 3.0 style session beans (with annotated local and remote business interfaces), or you create session beans with EJB 2.x style home and component interfaces (the four interface types, implementing the respective API). You can also create beans using a mixture of the two styles.

There are two types of session beans - stateful and stateless .

Stateful Session Beans

Stateful session beans have a state that is preserved between subsequent method invocations. This state is called conversational state. For example, you can store information in a bean class fields, which can be later used in subsequent business method invocation. The state is preserved as long as the client session lasts. Stateful session beans are associated with and represent a single client. Use stateful session beans when the bean is associated with and contains data about a specific client during method calls.

Stateless Session Beans

Stateless session beans have a simpler logic than the stateful session beans. It is not possible to preserve information between the invocations of the different business methods. The EJB Container chooses a random bean instance to execute a business method each time a request arrives. Use stateless session beans, when:

  • The methods of the bean do not depend on the specific client;

  • You require increased performance.

Stateless session beans provide better performance because they require fewer resources to support a large number of clients. However, you may have to develop more complex clients.

Procedure

  1. In the context menu of the EJB project, choose Start of the navigation path New Next navigation step Session Bean (EJB 3.x). End of the navigation path.
  2. Enter the bean settings as required.

    If you want your bean to have EJB 3.x style business interfaces, select the Local and/or Remote options, respectively.

    If you want to create a stateless session bean, select Stateless from the State type dropdown list.

    If you want to create a stateful session bean, select Stateful from the State type dropdown list.

  3. Choose Next .
  4. If necessary, enter the additional bean settings.

    If you want the bean to have EJB 2.x style interfaces, choose Home and Component interfaces (EJB 2.x) . In the fields that are displayed select the Local and/or Remote options respectively.

  5. Choose Finish .

Results

You can see the bean class and interfaces in the ejbModule node of the relevant EJB project in the Project Explorer . The number of generated bean interfaces depends on the respective options you chose in the wizard.

You can now edit the session bean and its interfaces.

Annotation Reference

Name

Use

Target

Annotation Attribute

@Stateless

Marks a bean class as a stateless session bean.

TYPE

name - this is the name of the bean class. The default value is "" .

mappedName - this is a product-specific name that the bean should be mapped to. The default value is "" .

description - this is a description of what a bean does. The default value is "" .

@Stateful

Marks a bean class as a stateful session bean.

TYPE

name - this is the name of the bean class. The default value is "" .

mappedName - this is a product-specific name that the bean should be mapped to. The default value is "" .

description - this is a description of what a bean does. The default value is "" .

@Init

Specifies that a method on the bean class corresponds to a create<METHOD> method for an adapted EJB 2.1 EJBHome and/or EJBLocalHome client view.

METHOD

value - specifies the name of the corresponding create<METHOD> method of the adapted home. You have to specify it when you use the @Init annotation with an adapted home interface of a stateful session bean that has more than one create methods. The Init method is only required for stateful session beans that provide a RemoteHome or LocalHome interface. If there is any ambiguity, you have to specify the name of the adapted create method of the Home or LocalHome interface. The default value is {} .

@Remove

Denotes a remove method of a stateful session bean. Completion of this method causes the container to remove the stateful session bean, first calling the bean's PreDestroy() method if any.

METHOD

retainlfException - allows the removal to be prevented if the method terminates abnormally. The default value is false .

@Local

@Remote

When you apply it to the bean class, this annotation lists the remote or local business interfaces of the bean in the value member. When you apply it to a business interface, the interface itself is marked as local or remote.

TYPE

value - this is an array of business interfaces and is specified only when the annotation is applied to the bean class. It is only required if the bean class implements more than one interface (excluding java.io.Serializable, java.io.Externalizable , and any of the interfaces in the javax.ejb package). The default value is {} .