Step 5: Create a New Entity Bean

In part one of the tutorial you created the entity bean Band by importing it from an EJB JAR file. It is also possible to generate an entity bean from an existing database table. In this step, however, you will develop your own entity bean from scratch.

The tasks in this step are:


To Create an Entity Bean

  1. Right-click the bands folder in the Application tab, and select New-->Entity Bean.
  2. Enter the file name RecordingBean.ejb.

  3. Click Create.


To Change the Data Source

You need to point the Recording bean to the data source that is defined in the workshop domain:

  1. Ensure that the Recording EJB is displayed in Design View.
  2. In the Property Editor, in the General section, locate data-source-name and change the entry to cgSampleDataSource.

Notice in same section of the Property Editor that the table-name is recordings. Also notice in the Naming section that the local interfaces and the local JNDI name have been automatically defined, as shown below.

To Define CMP Fields

Now you define the container-managed persistence (CMP) fields for the Recording EJB. Remember that CMP fields are virtual fields that are not defined in the entity bean itself but correspond to columns in the database table. The entity bean implements the accessor (getter and setter) methods to read and write to the table. By default these accessor methods are also added to the bean's local (business) interface. You will define two primary key fields that hold the name of the recording and the recording band:

  1. Ensure that the Recording EJB is displayed in Design View.
  2. Right-click the RecordingBean and choose Add CMP field. A CMP field called String field1 appears in the CMP Fields section.
  3. Rename the field String bandName. If you step off the field and need to rename it, right-click the field and select Change declaration.

    Note. If you stepped off the field and used Change declaration to rename the field, select the bandName declaration in Design View and go to the Property Editor to change the ejbgen:cmp-field tag's column property to bandName. (The name of the CMP field and the corresponding column name can be different, but for consistency reasons you should give them the same name here.)

  4. Right-click the field String bandName and select Primary Key. A key icon appears in front of the field definition.
  5. Repeat these steps to create the primary key field String title.
  6. Save your results.
  7. Go to source view and examine the code. Notice that the accessor methods have been defined, but not the fields, and that ejbgen tags are used to tag the primary key and to define the accessor methods in the bean's local interface.


To Add EJBCreate

Now you will add an ejbCreate method, which when invoked creates a new Recording bean and adds the corresponding record to the database table. When you follow the steps below to define ejbCreate, WebLogic Workshop automatically adds the corresponding ejbPostCreate to the bean class definition, and defines the create method in the home interface:

  1. Ensure that the Recording EJB is displayed in Design View.
  2. Right-click the RecordingBean and choose Add EJBCreate. The Add ejbCreate method dialog appears.
  3. In the top panel the correct definition of the ejbCreate method is already displayed. Verify that the method takes a band name and a title of the recording as arguments. Also verify that the order of the arguments is correct, as the code that you will add later assumes this order. Click OK.

  4. The method you created appears in the EJB Create Methods section. Click the method to go to Source View and examine the code.
  5. Save your results.


To Add a Relation

In the real world, recordings are made by bands. A band can have one or multiple recordings and, by and large, a recording is made by one band. (For now we will ignore the scenario where multiple bands contribute to a recording.) Here you will define this relationship in your Recording bean:

  1. Ensure that the Recording bean is displayed in Design View.
  2. Right-click the RecordingBean and choose Add Relation. The Add a Relation dialog appears.
  3. From the drop-down list in the right-hand panel select BandEJB as the Target EJB Name.
  4. In the Source CMR Field Name in the left-hand panel, enter band.
  5. In the Source CMR Field Type, select Band from the drop-down list.
  6. ln the Target CMR Field Name in the right-hand panel, enter recordings.
  7. In the Target CMR Field Type, select java.util.Collection from the drop-down list.

  8. Click Next.
  9. You can now decide whether the relationship is modeled using existing columns in the database table or whether you want WebLogic Workshop to create the necessary columns for you. Because Workshop is automatically creating the table for you, that is, the table for the Recording bean does not exist yet, select Workshop generates foreign key columns for recording.

  10. Click Next.
  11. Workshop automatically maps the Band bean's primary key name to a new column BandEJB_name in the Recording table. Click Next.

  12. Workshop provides a name for the relationship. Click Finish. The Recording-BandEJB relation appears in Design View in the Relations section.

  13. Right-click the Recording-BandEJB relation and choose Edit in source view to examine the ejbgen:relation tag. To learn more about this tag, place the cursor anywhere in the tag and press F1. Also scroll down and verify that the relationship accessor methods getBand and setBand have been automatically created. Invoking the getBand method returns a reference to a Band bean object, that is, a reference to the band who made this recording.
  14. The Recording-BandEJB relation is now also defined in the Band bean. To verify, go to the Band bean in Design View and observe that the relation is defined. Also, go to Source View to examine the ejbgen tag and the relationship accessor methods getRecordings and setRecordings. Invoking the getRecordings method returns a Java Collection containing references to all the relevant Recording beans, that is, all the recordings made by a particular band.

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, please run through the instructions in the current step again and verify that you have entered the correct information. 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).

Related Topics

Entity Relationships

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