Designing an EJB

Before you begin, you should have a general idea of what kind of EJB you plan to create. The EJBWizard supports the 4 major types of EJBs - although the "bean type" dropdown list shows the original 4 plus Message-Driven beans, MBD's aren't presently supported, since no templates or tabs have been designed for them (Contributions are welcome!). Here's how you use the EJBWizard tab-by-tab:

Bean tab

  1. Enter the EJB's name. This is like a Java classname, except that this name must begin with an uppercase letter. The EJBWizard will enforce this rule automatically.

  2. Enter the package name for the EJB. It's generally a good idea for each EJB to be in its own unique package.

  3. Define the bean type.

Data tab

The Data tab is mostly useful when defining Entity EJBs. However, it can also be used to define member fields (and their set/get methods) for Stateful Session EJBs. Stateless Session EJBs aren't allowed to have member fields.

You can define the fields in an EJB manually or by retrieving them from the definition of an existing database table. To do that, you must have defined the datasource(s) you wish to query in the $EJBWIZARD/datasources/ file. Click the "datasource" button to bring up the datasource dialog, select a database from the dropdown list, and click "login" to retrieve information about that database. When you click "OK", the fields of the database table you have selected will be replicated into the Data tab's field table display, erasing any previous definitions.


For Entity EJB's, you must define one or more fields as key fields. To do this, select a non-zero value from the dropdown list in cells of the "key" column. The EJB's primary key class will be defined by taking these fields in the order of their "key" value.

Please note that the field definitions allow you to define a short description of the field's purpose. It's a good idea to fill this information in, as it will be repeated in various parts of the generated EJB and any JavaDocs you may generate from it.

Although EJB 1.1 allows you to define the primary key in terms of a simple class object, the EJBWizard always generates the primary key in a "wrapper" class. You can edit the generated code if this is a problem.

Create tab

The Create tab is where you define the various create methods, analogous to Java class constructors - EJB's should not have constructors!

To define a create method, click on the "Add" button and fill in the dialog. The two most common create methods you will want to define are the no-arguments create method (just leave the argument list blank) and the all-fields create method. If you click on the "All fields" button, all of the fields defined on the Data tab will be added as arguments to the create method. You can also use this button as a shortcut - simply click "all fields", delete any fields that you don't want this method to use, and add any new fields. Use the "up" and "down" buttons to rearrange the order of arguments as needed. You can reduce the chances of mistyping a field definition this way.

Stateless session EJBs are not allowed to define create methods.

The Methods tab

Use this tab to define the business method prototypes. You will have to edit the EJBWizard-generated code to insert the actual logic, as the "Edit" button is there only for experimental purposes. Business methods are mostly found in Session EJBs, but it is sometimes useful to provide transfer mechanisms for Entity beans, such as methods to bulk-alter fields (in order to reduce RMI overhead) or to export the EJB's contents as an XML output string.

The Finder tab

Entity EJB's often define Finder methods in order to locate either a specific instance of an EJB or a collection of EJBs with some common attribute. The Method Editor for Finders works just like it does for create and business methods with 2 exceptions:

  1. The return type of the finder method must either be the EJB type, a java.util.Enumeration or a java.util.Collection (or derivative)

  2. You use the "SQL" button to bring up a dialog that defines how the EJB(s) are to be found

The SQL dialog is a generic text-entry field. Presently no validation is done. This is because in EJB 1.0 and 1.1 the finder code is appserver-dependent. For JOnAS EJBs, it's an SQL WHERE clause (including the introductory word "WHERE") or it's blank to find ALL EJB's. For WebLogic it's a WLCS expression such as "(< $0 100000)"

JSP considerations

The EJBWizard will create standard display/edit JSPs and a JSP controller class. In many cases these JSP's can be used with little or no change. For them to work correctly, however, you must define an "all-fields" create method and an "findAllXXXX" method (where "XXXX" is the bean name).

Generating the EJB

Once you've filled in the various tabs of the EJBWizard, you can then save the definition (in a ".jjb" file) and/or generate the EJB source files.

The File/Generate menu command will pop up a dialog that allows you to choose the project directory into which you will generate the EJB's directory and subdirectories. The initial value for this can be set on the command line with the "-o pathname" option. Otherwise it will default to the working directory.