# Step 6: Update the Band Entity Bean

In the previous step you defined an entity relation between the Band and Recording beans. In this step you will use this relation and modify the Band EJB to include business methods that create a new recording for a band and return a list of recordings for a band.

The tasks in this step are:

- To Reference the Entity Bean
- To Add an ejb-local-ref Tag
- To Add Component Methods
- To Build the EJBs

To Reference the Entity Bean

The Band bean will be calling methods of the Recording bean. To do so, the Band bean must first locate and obtain a reference to the Recording bean's home. In part one of the tutorial you created a similar reference in the stateless session bean Music by modifying its ejbCreate method, which is always called to obtain a reference to an instance of a Music bean. Unlike with session beans, the ejbCreate method is only invoked to create a new entity bean and is not used to obtain a reference to an existing bean; existing bean instances are returned by find methods (such as findByPrimaryKey). However, the setEntityContext callback method is always invoked for an entity bean during its life cycle, and is used in the Band bean to execute the JNDI API code to obtain a reference to the Recording bean. For more information on the life cycle methods of session and entity beans, see The Life Cycle of a Session Bean and The Life Cycle of an Entity Bean.

Before you modify Band bean's setEntityContext, verify the name of the Recording bean's home interface:

1. In the Application pane, double-click RecordingBean.ejb and ensure you are in Design View.
2. In the Property Editor, locate the Naming section and verify that only Local EJB is checked.
3. Expand the Local EJB section and verify that the local home interface is called RecordingHome.

Now modify setEntityContext to obtain a reference to the Recording bean's local home interface:

1. In the Application pane, double-click BandBean.ejb and select the Source View tab.
2. Locate the setEntityContext, and modify its definition as shown in red below to obtain a reference to RecordingHome:

         public void setEntityContext(EntityContext c) {
            ctx = c;
            try {
               javax.naming.Context ic = new InitialContext();
               recordingHome = (RecordingHome)ic.lookup("java:/comp/env/ejb/Recording");
            }
            catch(Exception e) {
               System.out.println("Unable to obtain RecordingHome: " + e.getMessage());
            }
         }

3. Go to the beginning of the class definition and define recordingHome as shown in red below:

       abstract public class BandBean implements EntityBean
       {
         private EntityContext ctx;
         private RecordingHome recordingHome;
         ...

To Add an ejb-local-ref Tag

In the above setEntityContext method you use ejb/Recording to reference the Recording bean. At deployment time this reference is mapped to the actual location of the Recording bean. This mapping is done in the deployment descriptor. As you already saw in part one of the tutorial, in WebLogic Workshop you don't modify the deployment descriptor directly but use ejbgen tags instead. To map the reference in the Band bean to the Recording bean, you need to insert an ejbgen:ejb-local-ref tag in BandBean.ejb:

1. Ensure that the Band 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 following properties in their respective fields, or go to Source View to add these properties:

        * @ejbgen:ejb-local-ref link="Recording"

To Add Component Methods

In this step you will add the business methods addRecording and getRecordingValues:

1. Ensure that the Band bean is displayed in Design View.
2. Right-click the BandBean and choose Add Component Method. A component method called void method() appears in the Component Methods section.
3. Rename the method void addRecording(String recording). If you step off the method and need to rename it, right-click the method and select Rename.
4. 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 recording) throws CreateException
          {
            Recording album = recordingHome.create(getName(), recording);
             Collection recordings = getRecordings();
             if(album != null) {
                recordings.add(album);
             }
          }

   Notice that the method uses the Recording bean's create method, which you defined in an earlier step, to add a new recording record to the database. Also notice that the method subsequently uses the relationship accessor method getRecordings to get a collection of references to all Recording beans for this band, and then adds the new recording to this collection. This last step ensures that the BandEJB_name column in the Recording table is set to the Band bean's primary key value. In other words, this ensures that the entity relation between the two beans is now also set for the new Recording, and that a subsequent call to the relationship accessor method getRecordings will return a list of recordings that includes the new recording.

5. Now repeat these steps for the method getRecordingValues. Go to Design View, right-click the BandBean and and choose Add Component Method. A component method called void method1() appears in the Component Methods section.
6. Rename the method Collection getRecordingValues().

   Note. Although you might be tempted to call this method getRecordings() instead, remember that earlier you already defined a (relationship accessor) method Collection getRecordings().

7. Click the component method to go to Source View and modify the method as shown in red below:

          /**
           * @ejbgen:local-method
           */
          public Collection getRecordingValues()
          {
             Collection result = new ArrayList();
             try {
                Collection fields = getRecordings();
                Iterator i_recordings = fields.iterator();
                while (i_recordings.hasNext()) {
                   Recording one_recording = (Recording)i_recordings.next();
                   result.add(one_recording.getTitle());
                }
             }
             catch(Exception e)
             {
                System.out.println("error getting recording infos: " + e.getMessage());
             }
             return result;
          }

   Notice that the method uses the relationship accessor method getRecordings to get a collection of all Recording beans related to this band. For each Recording bean it uses the method getTitle to retrieve the name of the recording. The method returns the collection of recording titles.

   Note. Instead of returning a collection of recording titles, a modeling alternative would be to return a collection of Recording beans instead, and have the calling method determine which specific properties it needs.

8. 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 Recording bean's name correctly. 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: