Step 7: Update the Session Bean

The Music session bean will be called by a page flow application to work with band and recording data. Here you will add component methods to work with recording data. The tasks in this step are:

To Add Component Methods

In this step you add the corresponding business methods addRecording and getRecordings to the Music bean:

  1. Ensure that the Music bean is displayed in Design View.
  2. Right-click the MusicBean and choose Add Component Method. A component method called void method() appears in the Component Methods section.
  3. Rename the method void addRecording(String band, String recording).
  4. Right-click the arrow to the left of the component method and select Local. The business method is now defined in the local interface.
  5. Click the component method to go to Source View and modify the method as shown in red below:
       /**
        * @ejbgen:local-method
        */
       public void addRecording(String band, String recording) {
          try {
             Band bandBean = bandHome.findByPrimaryKey(new BandPK(band));
             if(bandBean != null) {
                bandBean.addRecording(recording);
             }
          }
          catch(CreateException ce) {
             throw new EJBException(ce);
          }
          catch(FinderException fe) {
             throw new EJBException(fe);
          }
       }

    Notice that the method uses the Band bean's findByPrimaryKey method to locate the band bean and uses the band bean's method addRecording to add the recording to the database.

  6. Now repeat the above steps for the getRecordings 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.
  7. Rename the method Collection getRecordings(String band).
  8. Right-click the arrow to the left of the component method and select Local.
  9. Click the component method to go to Source View and modify the method as shown in red below:
       /**
        * @ejbgen:local-method
        */
       public Collection getRecordings(String band) {
          Collection result = new ArrayList();
          try {
             Band theBand = bandHome.findByPrimaryKey(new BandPK(band));
             result.addAll(theBand.getRecordingValues());
          } 
          catch(Exception e) {
             System.out.println("error getting recordings from music bean: " + e.getMessage());
          }
          return result;
       }

    Notice that the method uses the Band bean's findByPrimaryKey to locate the band bean and uses the band bean's method getRecordingValues to receive a list of the band's recordings.

  10. 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 build errors related to the component methods, verify that the methods have been defined correctly. 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: