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:
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:
Now modify setEntityContext to obtain a reference to the Recording bean's local home interface:
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()); } }
abstract public class BandBean implements EntityBean { private EntityContext ctx; private RecordingHome recordingHome; ...
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:
* @ejbgen:ejb-local-ref link="Recording"
In this step you will add the business methods addRecording and getRecordingValues:
/** * @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.
Note. Although you might be tempted to call this method getRecordings() instead, remember that earlier you already defined a (relationship accessor) method Collection getRecordings().
/** * @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.
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: