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.

Before you begin

You must have a Java project, an EJB project, or a web project created in your workspace.

Procedure

  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 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 quick-fix icon next to 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:
    Stateful quick fix
  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 quick-fix icon next to the @Stateful line.
  9. Right-click the quick-fix icon and select Quick Fix:
    Quick fix menu item
  10. Select Import 'stateful' (javax.ejb) and press CTRL+S to save:
    Quick fix for Session bean
  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, after the package declaration. When you press CTRL+S to save, you can see a quick-fix icon quick-fix icon with 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 quick-fix icon with 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 quick-fix icon with 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, after the package declaration. When you press CTRL+S to save, you can see a quick-fix icon quick-fix icon with 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 quick-fix icon with 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 quick-fix icon with 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.
Icon that indicates the type of topic Task topic
Timestamp icon Last updated: July 17, 2017 21:58

File name: tcreatingstatefulsess.html