Step 2: Import an EJB from a JAR

If you have developed Enterprise JavaBeans in another development environment and plan to continue development of these EJBs in WebLogic Workshop, you can import these beans into an EJB project. To do so you must have the EJB JAR and the source code of the EJBs. An EJB Jar typically will contain multiple EJBs that can be imported at the same time. In this step of the tutorial you will import one entity bean.

The tasks in this step are:

Note. If you have developed EJBs in another development environment and plan to reuse these EJBs in a WebLogic Workshop application, but are not planning to continue development on these beans, you only have to add the EJB JAR to the application. For more information, see How Do I: Add an Existing Enterprise JavaBean to an Application?

To Import an EJB

  1. On the Application tab, right-click the MyEJBProject folder and select Import EJB from jar. The Import Wizard appears.
  2. Click the Browse button next to the top field and select the EJB JAR:
    [BEA_HOME]\weblogic81\samples\platform\tutorial_resources\EJBTutorial\import_jar\ejb20_relationships_bands.jar
  3. Click the Browse button next to the bottom field and select the source directory:
    [BEA_HOME]\weblogic81\samples\platform\tutorial_resources\EJBTutorial\import_src_dir
  4. Click Next.
  5. In Confirm EJB source file locations, confirm that the BandBean is checked and click Finish.
  6. An Import Progress window appears. When the import is complete, click OK.


To Inspect the Imported Entity Bean

The Application tab displays the BandBean that has been imported. Notice that the package name of the bean is reflected in the directory structure. In addition to the entity bean, a regular java class, which will be used as a dependent value class, has been imported.

The Band EJB in Design View should look like this:

If you have developed Enterprise JavaBeans before, you might notice that the BandBean entity bean is represented by one file with an ejb extension, instead of several java files for the class definition and the definition of the various interfaces. In addition, when you double-click BandBean.ejb and go to Source View, you will notice special tags with the prefix ejbgen. These ejbgen tags are for instance used to mark methods to be exposed in remote and home interfaces, mark relationship fields, and describe EJB QL queries directly in the bean class. Instead of using separate classes for the various interfaces and instead of directly changing the deployment descriptor, WebLogic Workshop uses these tags to contain all the bean's information in one file. When you build an EJB Project, the WebLogic tool uses these tags to generate the various interfaces and to generate the appropriate deployment descriptor. For more information, see Getting Started with EJB Project or the Getting Started: Enterprise JavaBeans tutorial.

The Band bean defines three CMP fields to represent bands: (1) the band founder, (2) the name of the band, and (3) the start date when the band was founded. The name field is the primary key field. In other words, the assumption is made that each band has a unique name. All fields are read-only, that is, no setter methods are defined for these fields to make changes. The Band bean has three methods. There is one ejbCreate method to create a new band object, and there are two finder methods: the findByName methods returns the band object that matches a name argument, and the findAll method returns all band objects stored in the database. The Property Editor reveals that the ejb-name is BandEJB, the data-source-name is cgSampleDataSource and the table-name is bands. Also notice in the JAR Settings section that create-tables is set to CreateOnly. The latter setting means that when you build and deploy this bean, the table will be automatically created for you. To learn more about the other table creation options set with this property, see @ejbgen:jar-settings Annotation. Notice that the create-tables setting is part of the JAR settings. In other words, for every entity bean created as part of this project, the same table creation setting applies.

To Build the EJBs

In this tutorial you will be building the EJBs frequently to verify that changes were made correctly:

  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 application is automatically deployed on the server: 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 and the following deployment error message appears.



    This deployment error message states that a link could not be resolved. You will define this link in the next step of the tutorial.
  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, verify that both BandBean.ejb and BandInfo.java were imported successfully. If this is not the case, delete the imported files and repeat the import steps above. In particular make sure that you are pointing at the correct folder containing the EJB source files.

If you encounter a deployment error in addition to the expected deployment error described above, it might be related to the underlying data source: verify that you are working in the workshop domain by selecting Tools-->WebLogic Server-->Server Properties. The Server Home Directory should point to the workshop domain.

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