This document provides a quick tutorial that teaches you how to use the SilverStream Application Server IDE to build and deploy an entity bean using container-managed persistence (CMP). This quick start includes these sections:
For more information on SilverStream support for EJBs, see the chapter on using EJBs in the Programmer's Guide. For more information on entity beans, see the chapter on writing entity beans in the Programmer's Guide.
This quick start assumes you have:
You do not need any EJB knowledge. But keep in mind that the intent of this quick start is to teach you how to use SilverStream to build CMP entity beans; it is not intended to teach you EJB theory or best practices.
Before doing the quick start exercises, you must set up your environment.
Add the EJB Examples database to your SilverStream server, if it isn't already.
The EJB Examples database is a Sybase Adaptive Server database that is installed and configured as part of the SilverStream Full or Typical install. You can use the username dba and password sql to add it to your server.
For information on adding a database, see the chapter on the Main Designer in the online Tools Guide.
Open the Objects directory of the EJB Examples database, but do not select anything.
If you have an item selected, you will not be able to create a top-level package. To unselect an item, press Ctrl and click the selected item.
Create a top-level package called quickstart in the Objects directory of the database. (This package may exist already if you have done the EJB Session Bean Quick Start.)
For information on creating packages, see the chapter on the Business Object Designer in the online Tools Guide.
Select the quickstart package, then create a subpackage called companydemo.
The entity bean you will build retrieves a row from the company table. You can retrieve the row by company ID (the primary key) or by company name. The Create EJB Wizard lets you use a database table as a template for generating code for the data you want to access. The entity bean is not tied to that table until you create the deployment plan. At that time, you can choose to map the bean to any appropriate table.
You will create the following entity bean components in the quickstart.companydemo package:
In addition, you will create the following JAR file:
|
File |
Description |
|---|---|
The EJB JAR file. It will contain the bean class, its primary key class, its home and remote interfaces, and a deployment descriptor. |
You will call the entity bean from the following SilverStream HTML page:
To create the EBcompaniesBean class and its home and remote interfaces:
Choose the quickstart.companydemo package you created in Setting up your environment.
Choose the New icon, then choose New Enterprise JavaBean to invoke the Create EJB wizard.
Follow the instructions for the wizard panes using these values:
Select the companyid and companyname fields and click > to put them in the right pane.
The Create EJB wizard displays the following pane:
The EJB Create Wizard constructs the following:
It opens EBcompaniesHome for editing.
To complete the EBcompaniesHome interface:
In the Programming Editor, add the following import statement:
import java.util.Collection;
Add the findByLikeness method to the home interface as follows:
public Collection findByLikeness(String psCompanyName) throws FinderException, RemoteException;
Save the EBcompaniesHome interface and exit the Business Object Designer.
Now you will deploy the entity bean and its components by:
Creating an EJB JAR and completing a deployment descriptor with transaction attributes
Creating a deployment plan (which includes mapping the persistent fields to the table the bean will access)
To create the deployment descriptor:
Choose List View at the bottom of the window (if not already chosen).
SilverStream creates an entry called UntitledEntityBean.
Provide the bean name, bean class, home interface class, and remote interface class as shown in the following Property Inspector.
You can choose the ellipses button (...) and select the correct entry from the dialog for the bean class, home interface class, and remote interface class.
NOTE The rest of the fields are handled in the following steps.
Before specifying Primary key class, change the Persistence type field to Container-managed.
Leaving the Property Inspector open, choose Persistent Fields (on the Descriptor pane of the JAR Designer), right-click and choose Add.
Go back to the Property Inspector and in the Primary key class field, type java.lang.Integer.
In the Primary key field field, choose the ellipsis button, then choose m_companyid.
To specify transaction attributes:
Choose Transactions (located under Application Assembly in the JAR Designer), right-click and choose Add.
SilverStream creates an UntitledTransaction entry.
Choose Edit Methods. If Edit Methods is not visible, choose the + Methods icon first.
SilverStream displays the Edit Methods dialog.
Choose the * under EBCompanyBean, choose > to add it, then choose OK.
This means that all of the EBCompanyBean methods execute with the transaction attribute Required.
Close the Property Inspector and save the EJB JAR.
SilverStream validates the EJB JAR and displays any errors or warnings in a Validation Status dialog. This dialog provides helpful information about the errors and warnings which can help you locate and fix them. You should not encounter any validation errors in this exercise.
An EJB deployment plan provides additional information that the SilverStream server needs to properly execute the EJBs within the EJB JAR.
To create the EJB deployment plan:
Right-click and choose Create EJB JAR Deployment Plan.
SilverStream launches the Deployment Plan Designer.
Right-click and choose Properties to open the Property Inspector.
Specify these values in the Bean tab of the Property Inspector:
In the Property Inspector, click the Edit Methods link (located below Methods that modify no fields).
In the Edit Methods dialog, add the getCompanyid() and getCompanyname() methods to the right pane and click OK.
The ability to specify the methods that do not modify fields is a SilverStream-specific deployment feature.
For more information on methods that do not modify fields, see the chapter on J2EE archive deployment in the Facilities Guide of the server's Core Help.
With the Property Inspector open, click each of the persistent fields in the Deployment Plan Designer and map them to the database columns as follows:
Choose the findByLikeness Finder method in the Deployment Plan Designer and specify these values in the Property Inspector:
Close the Property Inspector and save the deployment plan.
SilverStream prompts for a deployment plan name.
Accept the default name (QuickStartEBDeplPlan) and choose OK.
NOTE It is necessary to save the plan once before choosing Save and Deploy in the next step.
Choose File>Save and Deploy to create the remote JAR and deploy the main JAR to the server.
SilverStream prompts you for names for the deployed object and the remote access JAR.
SilverStream launches the rmi2iiop compiler, which creates the classes needed for the beans in the EJB JAR. Then SilverStream displays this message:
The JAR was successfully saved and activated.
You should not encounter any warnings or errors.
Now you can see the objects created during the deployment process. They are located in the JARs directory and include:
Before you can call the EBCompanyBean you just deployed, you need to modify the SilverStream page (pgCompanyQuickStart.html) that resides in the EJB Examples database. This page will not yet compile because you need to add code that:
To modify the pgCompanyQuickStart.html page:
In the General/Imports section, search for this comment (you need to be in whole file view to do a search. You can change the view with the buttons on the right above the editing area.):
//************************************************************* // For Quick Start imports // Insert code below //*************************************************************
Add this import statement below the comment:
import quickstart.companydemo.*;
Add to the page the EJB remote JAR that you created earlier by choosing File>Jar Files and adding the following JAR file:
ExamplesXX_EJB/QuickStartEBRemote.jar
In the page's General/getTheEntityBeanHome method, locate the Java comment that says:
//************************************************************** // For Quick Start JNDI lookup // Insert code below //****************************************************************
Add the following code which does the JNDI lookup() for the bean:
Object objEntityBeanLookup = initialContext.lookup("RMI/EBCompaniesBean");
The RMI/ specifies the root of the JNDI context where SilverStream stores EJB home references when the EJBs are deployed. You must use it with all SilverStream JNDI EJB lookups. EBCompaniesBean is the JNDI name you specified in the Deployment Plan Designer.
Still in the page's General/getTheEntityBeanHome event, locate the comment that says:
//************************************************************* // For Quick Start Home Object // javax.rmi.PortableRemoteObject.narrow() // Insert code below (before the return statement) //*************************************************************
Add this line of code to narrow (not cast) the returned EJBHome object to your user defined type:
m_ebCompanyHome = (EBcompaniesHome) javax.rmi.PortableRemoteObject.narrow(objEntityBeanLookup, quickstart.companydemo.EBcompaniesHome.class);
In the handle_btnFindLikeName_pageActionPerformed method, locate the comment that says:
//************************************************************* // For Quick Start Remote Object // javax.rmi.PortableRemoteObject.narrow() // Insert code below (before the first sHTML statement) //*************************************************************
Add this line of code to narrow (not cast) the returned EJBObject to your user defined type.
EBcompanies ebcompanies = (EBcompanies) javax.rmi.PortableRemoteObject.narrow(objEntityBeanCompany, quickstart.companydemo.EBcompanies.class);
The EJB1.1 Specification requires that you use the javax.rmi.PortableRemoteObject.narrow() method because it works with both JRMP and RMI-IIOP protocols. This allows your code to work with EJBs on any type of EJB-compliant server.
To get a company by Company ID:
Enter 1 in the Company ID field, then click the Find Using Company ID button.
The page should return Computers R Us in the Company Name field.
This illustrates the findByPrimaryKey() method.
Click the Find Using Company Name button.
The page should return the following companies:
4 Always Publishing Corporation 5 ACME Fireworks 7 Alot of Java
This illustrates the findByLikeness() method.
In this Quick Start, you created the following objects:
Copyright © 2001, SilverStream Software, Inc. All rights reserved.