Step 3: Create a Session Bean

In this step you will create a stateless session bean Music, which will be called by client applications. A client application has no direct access to the entity bean Band but calls the session bean instead. The Music bean implements the business logic to respond to a client request, invoke the Band bean to obtain data, and return a result to the client. This use of a session bean as an intermediary is a common design pattern called Session Façade.

The tasks in this step are:


To Create a Session Bean

  1. Right-click the bands folder in the Application tab (the same folder that contains BandBean.ejb and BandInfo.java), and select New-->Session Bean.
  2. Enter the file name MusicBean.ejb.

  3. Click Create.


To Reference the Entity Bean

The Music EJB will be invoking methods of the Band EJB. In order for the Music bean to invoke the Band bean's methods, it must first locate and obtain a reference to the bean's home. In this step, you modify the ejbCreate method in MusicBean.ejb to locate and obtain a reference to the Band EJB.

Note. To learn more about about EJB interfaces, referencing and JNDI naming, see the Getting Started: Enterprise JavaBeans tutorial.

To modify ejbCreate to obtain a reference to the Band EJB's local home interface:

  1. In the Application pane, double-click MusicBean.ejb and select the Source View tab.
  2. Locate the ejbCreate method in MusicBean.ejb, and modify its definition as shown in red below. You might notice that the method uses Java JNDI to locate and obtain a reference to BandHome:
       public void ejbCreate() {
          try {
             javax.naming.Context ic = new InitialContext();
             bandHome = (BandHome)ic.lookup("java:comp/env/ejb/BandEJB");
          } 
          catch (NamingException ne) {
             throw new EJBException(ne);
          }
       }
  3. Locate the import section at the top of the file and add the following import statements, or use Alt+Enter when prompted by the IDE to automatically add these import statements:
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
  4. Go to the beginning of the class definition and define bandHome as shown in red below:
    public class MusicBean 
      extends GenericSessionBean 
      implements SessionBean
    {
      private BandHome bandHome;	   
    
      public void ejbCreate() {
      ...

To Add an ejb-local-ref Tag

In the above ejbCreate method, in the lookup method, you use BandEJB to reference the Band EJB. At deployment time this reference is mapped to the actual location of the Band bean. This mapping is done in the deployment descriptor. In WebLogic Workshop you don't modify the deployment descriptor directly but use ejbgen tags inserted in the ejb file instead. To map the reference to Band EJB in the Music bean to the actual Band bean location, you need to insert an ejbgen:ejb-local-ref tag in MusicBean.ejb:

  1. Ensure that the Music EJB is displayed in Design View.
  2. Right-click the Design View, and select Insert EJB Gentag-->ejb-local-ref. A new ejbgen:ejb-local-ref section appears in the Property Editor.
  3. Use the Property Editor to enter the link property, or go to Source View to add the property:
     * @ejbgen:ejb-local-ref link="BandEJB"

    Notice that the attribute specified in the link property corresponds to the ejb-name of the Band bean. To learn more about this tag, place your cursor anywhere inside the tag in Source View and press F1.

To Add Component Methods

Now you will add the business methods to the Music bean that will be called by the client application. Before you add the getBands and addBand methods, you first need to define Music bean's local interfaces:

  1. Ensure that the Music EJB is displayed in Design View.
  2. In the Property Editor, locate the Naming section. Clear the Remote EJB and check the Local EJB. In other words, for the Music EJB you will use its local interfaces.
  3. Expand the Local EJB section and enter ejb.Music in the JNDI name field. Verify that the bean class name is MusicLocal, and the home class name is MusicLocalHome, as is shown in the following picture:

  4. In Design View, right-click the MusicBean and choose Add Component Method. A component method called void method() appears in the Component Methods section.
  5. Rename the method Collection getBands(). If you step off the method and need to rename it, right-click the method and select Rename.
  6. Right-click the arrow to the left of the method and select Local. The business method will now defined in the local interface during build.
  7. Click the component method to go to Source View and modify the method as shown in red below:
       /**
        * @ejbgen:local-method
        */
       public Collection getBands() {
          try {
             Iterator bands = bandHome.findAll().iterator();
             Collection result = new ArrayList();
             while (bands.hasNext()) {
                Band band = (Band)bands.next();
                result.add(band.getName());
             }
             return result;
    } catch (FinderException fe) { throw new EJBException(fe); }
    }

    Notice that the getBands method calls uses the BandBean to obtain all the bands currently stored in the database. The method uses the Band EJB's findAll method to find all bands, and returns a Collection of band names. To learn more about finder methods, see Query Methods and EJB QL.

  8. Locate the import section at the top of the file and add the following import statements, or use Alt+Enter when prompted by the IDE to automatically add these import statements:
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
  9. Now repeat steps 4 through 7 for the addBand method. Go to Design View, right-click the MusicBean and choose Add Component Method. A component method called void method1() appears in the Component Methods section.
  10. Rename the method void addBand(BandInfo bandInfo).
  11. Right-click the arrow to the left of the component method and select Local.
  12. Click the component method to go to Source View and modify the method as shown in red below:
       /**
        * @ejbgen:local-method
        */
       public void addBand(BandInfo bandInfo) {
          try {
             bandHome.create(bandInfo.getName(), bandInfo.getFounder(), bandInfo.getStartDate());
          } 
          catch (CreateException ce) {
             throw new EJBException(ce);
          }
       }

    Notice that the method calls the Band EJB's create method to add a new band to the database.

  13. Save your results.

To Build the EJBs

  1. Locate the Build window in the IDE. If the Build window is not there, select View-->Windows-->Build.
  2. In the Application pane, right-click MyEJBProject, and select Build MyEJBProject.
  3. Monitor the Build window and verify that the build completes correctly, and that the EJB JAR MyEJBProject.jar is created.
  4. After the build completes, the EJBs are deployed: Watch the green ball in the WebLogic Server status bar at the bottom of WebLogic Workshop turn yellow with the message Updating Server. Wait until the ball turns green again. The two EJBs are now deployed on the server. In addition, the various web applications that were predefined are deployed to the web container. We will turn to these in the next step.
  5. (Optional.) Verify the contents of the EJB JAR. In the Application pane, expand Modules, expand MyEJBProject.jar, and expand the directory structure reflecting the package name. Verify that the bean classes and the various interfaces have been created.

If you encounter a build error related to the ejbgen:ejb-local-ref tag, make sure that you have specified the BandBean's name correctly. If you encounter build errors related to the component methods, verify that the methods have been defined correctly and that you have imported the correct classes. A build error might be followed by a deployment error, in particular for WebAppOne_Standard and related to an unresolved ejb-link. The link cannot be resolved because the EJB JAR was not created. Fixing the build problem should resolve this deployment error.

If you encounter deployment errors that seem to be related to one of the web applications, make sure that you build the EJB project and not the entire application (because the application contains page flow applications that have different EJB dependencies from what you have created in this step).

Click one of the following arrows to navigate through the tutorial: