Creating a stateful session bean using annotations

You can use Java™ EE annotations to create a stateful session bean and add it to your project.

You must have a Java project or an EJB project created.

To create a stateful session bean in your EJB project:

  1. In the Java EE perspective, click

    File | New | Class. The Create a New Java Class wizard opens.

  2. In the

    Source folder field, select the source folder for the new bean.

  3. In the

    Java package field, type the package name for the new bean.

  4. In the Bean name field, type the name that you want to assign to the enterprise bean. By convention, bean names should 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 Java class editor, underneath the package declaration, type @stateful. You can see an error / quick-fix icon beside the @stateful line.

    Tip: You can simply type @Sta and then press CTRL+Spacebar to see the options in context assistance:

    Select

    @Stateful(EJB) - javax.ejb to create a stateful session bean.

  6. Press CTRL+Spacebar to see the options in context assistance:

  7. Select

    @Stateful(EJB) - javax.ejb to create a stateful session bean.

  8. When you press CTRL+S to save, you can see a quick-fix icon beside the @Stateful line.

  9. Right-click the quick-fix icon and select

    Quick Fix:

  10. Select

    Import 'stateful' (javax.ejb) and press CTRL+S to save:

  11. In the Enterprise Explorer view, expand your

    <EJB project_name> | ejbModule, and your new session bean Java class is listed under its package name.

  12. Define the client views and interfaces. For EJB 3.0 or later beans, you can include a remote client interface, a local interface, or both. Here is an example of a basic Remote interface:

    package com.ibm.test;
    import javax.ejb.Remote;
    
    @Remote
    public interface TestBeanRemote {
    
    }

    • Remote client interface: To create a remote client interface:

      1. Right-click your EJB project, select click

        New | Interface. In the Create a New Java Interface wizard, type the package name of your session bean in the

        Package field. Type a name for your interface in the

        Name field and click Next.

      2. In the Interface editor, type @Remote to your new remote interface, underneath the package declaration. When you press CTRL+S to save, you can see a quick-fix icon beside the @Remote line. Right-click the quick-fix icon and select

        Quick Fix, select

        Import 'Remote' (javax.ejb) and press CTRL+S to save.

      3. To add a Remote home interface, add the annotation @RemoteHome to your session bean class. When you press CTRL+S to save, you can see a quick-fix icon beside the @RemoteHome line. Right-click the quick-fix icon and select

        Quick Fix, select

        Import 'RemoteHome' (javax.ejb) and press CTRL+S to save. When you press CTRL+S to save, you can see a quick-fix icon beside the @RemoteHome line. Right-click the quick-fix icon and select

        Quick Fix, select

        Add missing attributes. Provide the values for the name-value pair: (value=null), and press CTRL+S to save.

    • Local client interface: To create a local client interface:

      1. Right-click on your EJB project, select click

        New | Interface. In the Create a New Java Interface wizard, type the package name of your session bean in the

        Package field. Type a name for your interface in the

        Name field and click Next.

      2. In the Interface editor, type @Local to your new local interface, underneath the package delcaration. When you press CTRL+S to save, you can see a quick-fix icon beside the @Local line. Right-click the quick-fix icon and select

        Quick Fix, select

        Import 'Local' (javax.ejb) and press CTRL+S to save.

      3. To add a Local home interface, add the annotation @LocalHome to your session bean class. When you press CTRL+S to save, you can see a quick-fix icon beside the @LocalHome line. Right-click the quick-fix icon and select

        Quick Fix, select

        Import 'LocalHome' (javax.ejb) and press CTRL+S to save. When you press CTRL+S to save, you can see a quick-fix icon beside the @LocalHome line. Right-click the quick-fix icon and select

        Quick Fix, select

        Add missing attributes. Provide the values for the name-value pair: (value=null), and press CTRL+S to save.

 

Related reference

Common Annotations