Creating session beans

You can use a wizard to create a session bean and add it to your project.

Before you begin

About this task

Beginning with the EJB 2.1 specification, stateless session beans can include a Web service client view, as well as the local and remote views. A Web service client view includes a single service endpoint interface. You can use the wizard to generate the service endpoint interface for EJB 2.1 stateless session beans.

Procedure

  1. In the Java™ EE perspective, click File > New > Other. Select EJB > Enterprise Bean (1.x-2.x) and click Next. Alternatively, right click your EJB project and click New > Enterprise Bean (1.x-2.x).
  2. Select Session Bean.
  3. Select the EJB Project that you want to add the bean to.
  4. In the Bean name field, type the name that you want to assign to the enterprise bean. By convention, bean names begin with an uppercase letter.
    Note: You can use Unicode characters for the bean name, but Unicode characters are not supported for enterprise bean packages and classes associated with enterprise beans.
  5. In the Source folder field, select the source folder for the new bean.
  6. In the Default package field, enter the package name for the new bean.
  7. Optional: For EJB 2.x beans, you can select Generate an annotated bean class. If you select this option, the wizard generates annotations at the beginning of the Java code for the bean class. The annotations define the implementation of the bean as you specify in the wizard. The annotations are then used by the EJB tools to generate the necessary bean classes, and to provide values that are inserted in the EJB deployment descriptor (ejb-jar.xml). Select this option if you are familiar with annotations and want to use the annotations to update the bean rather than using the deployment descriptor.

    For more information, see Annotation-based programming overview.

  8. Click Next.
  9. Select one of the following session types for the new bean:
    • 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 and instances of a stateless session bean do not maintain conversational state. All instances of a stateless session bean have the same object identifier, which is assigned by the container.
  10. Select one of the following transaction types for the new bean:
    • Container: Specifies that the transaction demarcation is performed by the container.
    • Bean: Specifies that the transaction demarcation is performed by the bean.
  11. Optional: If you want the new bean to inherit from an existing bean in the same EJB project, select a Bean supertype. The Bean supertype drop-down box is enabled if other Session beans exist in the EJB project. Selecting a Bean supertype disables the Key class field since subtypes must have the same key class as the supertype.
  12. In the Bean class field, enter the package and class name for the bean class. By default, the wizard suggests a bean class based on the bean name and default package that you defined. A bean class can be a new class that the wizard generates, or it can be an existing class in the project class path. Click the Class button to open the Type Selection window that lists the classes in the project that correspond to the bean type. The name of the bean class appears blue for existing classes with source. The name of the bean class appears red for existing binary classes.
  13. Define the client views and interfaces. For EJB 2.0 or later beans, you can include a remote client view, a local client view, or both. For EJB 1.1 beans, only a remote client view is supported. Every session or entity bean must have at least one client view:
    • Remote client view: Select this check box to include a remote client view for the session bean.

      In the Remote home interface and Remote interface fields, enter the package and class names that you want to use for the remote client view interfaces. The wizard uses the bean and package names to suggest values for the interface package and class names.

    • Local client view: Select this check box to include a local client view for the session bean.

      In the Local home interface and Local interface fields, enter the package and class names that you want to use for the local client view interfaces. The wizard uses the bean name and default package to suggest values for the interface package and class names.

    • Service client view: For EJB 2.1 projects only, you can select this check box to include a Web service client view for the session bean.

      In the Service endpoint interface field, enter the package and class name that you want to use for the service client view endpoint interface. The wizard uses the bean name and default package to suggest a value for the interface package and class name.

      Note: The Service client view check box is not available if you selected Generate an annotated bean class on the previous page. If you want to generate a service client view, you cannot use the annotations.
    Click Next.
  14. Optional: In the Bean superclass field, type or select the class that you want.
  15. Optional: Define any interfaces that you want the remote or local client interfaces to extend:
    • Click the Add button to open the Type Selection window where you can select the interface that you want to extend.
    • If you added an interface that you no longer want to extend, select the interface in the list and click Remove.
    Click Next
  16. Optional: To Add bean to Class Diagram, select this check box and select a class diagram from the explorer view of your projects. There is a default Class Diagram already created in each EJB project. If you want to create a new Class Diagram, click New to open the New Class Diagram wizard.
  17. Click Finish to add the new bean to the specified EJB project.

Feedback