Pramati Technologies

Creating Entity Beans with CMP 2.0

The deployment descriptors for the CMP bean has substantially changed in the EJB 2.0 specification. The CMP entity bean can be defined as the contract between an entity bean and its container. The contract defines the life cycle of the entity bean instances, the model for method delegation of the business methods invoked by the client, and the model for the management of the entity bean's persistent state and relationships.

The main goal of this contract is to ensure that an entity bean component using container-managed persistence is portable across all compliant EJB Containers. This chapter describes how to use the bean wizard to automate the complex process of building such components.

Starting Bean Wizard

Start Bean wizard using Tools > Bean Wizard from the main menu. The Bean Wizard can be run in two modes:

Expert Mode takes the user on an exhaustive eleven-step path to writing a customized bean. Quick Mode takes the user on a simpler four-step path to creating a bean with standard interfaces, and does not include creating ejbHome, ejbSelect and the business method skeletons.

The following sections describe all the steps involved in creating CMP 2.0 beans in the Expert mode.

Defining Beans

In the first panel that comes up, define the bean as follows:

Module Name All the EJB modules existing in the active Desk are displayed here. Choose one from the list available in the combo-box. The bean that is created is placed under the module chosen here.

Bean Type Select the bean type as Entity - Container Managed 2.0. This field defines the type of bean to be created. The user can choose from a list of types available in the combo box.

Transaction Type Transactions are defined only for session beans and message driven beans. This field is therefore disabled when the user chooses Entity -Container Managed 2.0 as the bean type.

Use Database Check against this checkbox if you decide to use the existing database available. To generate code when a database does not exist, un-check the checkbox. This checkbox is checked, by default.

Re-entrant This checkbox is enabled only for entity beans and by default is un-checked. The entity bean when specified as a non-reentrant, makes the container reject any attempt made by entity bean's component interface while the instance is executing a business method.

Bean Class Name The name of the bean class is entered in this field. If the bean is included under a package, the complete Java class name, including the package name, must be entered.

EJB Name The logical name assigned to each bean in the ejb-jar is entered here. This name is automatically added when the developer adds the bean class name. The developer, if required can modify the name.

Abstract Persistence Schema Name This field specifies the name of the abstract schema type and is required only for CMP entity 2.0 beans. This is used in EJB QL Queries.

JNDI Name Entering the bean class name automatically adds the JNDI name in this field. The developer, if required can modify the name. The JNDI Name locates the entity bean's home interface. This name is linked to the actual JNDI Name of the target enterprises' home interface during deployment.

The JNDI Name is named as [Bean Name_Module Name_Desk Name], where Bean Name is the name of the bean, Module Name is the name of the bean and Desk Name is the name of the Desk. Naming the JNDI Name with all these components makes it unique.

Expert Mode Beans in Studio can be created in two modes - Expert and Non-Expert. The Expert Mode guides the user through all the 12 steps of creating a bean. This is to be used by experienced developers, who want to create the bean with specific details.

The Non-Expert mode guides the user in 4 simple steps and creates the cmp 2.0 bean. This mode gives default values for the interfaces and also does not create methods like ejbSelect. The following are the panels, which appear in the Non-Expert mode of the Bean Wizard.

  1. Define Container Managed Fields
  2. Define Container Managed Relationships
  3. Define CREATE Methods
  4. Define FINDER Methods

Once all the fields have been filled, click Next. This takes the user to the next panel. The next panel can be either of the two panels given below:

In the following sections, the Expert Mode for creating Container Managed Entity 2.0 Beans is discussed. For creating the Bean using the Non Expert mode, just un-check the option Expert Mode and read the specific panel headings

Defining Container Managed Fields (with database)

If the option Use Database is checked in the Bean Definition panel, this is the next panel to come up. Enter the following information in this panel:

Resource Name Select from the existing data resource names available in the dropdown combo box.

The user can add the CMR field after generating the bean sources by adding both the abstract get/set methods in the bean's source code, and then compiling the bean.

URL The location of the driver is displayed here. This field cannot be edited.

Schema Filter This field is used to filter the tables depending on the schema names in all the databases. When the database in use is Oracle, the schema name provided in the drop down combo box is ignored and the "User name" used for creating the resource is used as schema name.

Table Filter This field is used to filter and display the tables in the Tables field.

Example

To view all tables starting with the letters IS, type is* in this field and click "Get". This displays all the tables starting with IS in the Tables field.

To view all the existing tables in the database, type * in the Table Filter field.

Tables Select from one of the tables in the dropdown combo box. This displays all the columns in the table. Use hot keys to jump to the desired table name in the list.

CMP Fields Check the options under this field, which are to be persisted.

DB Field Name This field displays the database fields of the tables existing in the database. This field is not editable.

DB Field Type This field displays the database type of the fields. This field is not editable.

DB PK The database fields, which act as the primary key, have a yellow key as an icon. This field is not editable.

Bean Field Name This field displays the Bean field names.

Bean Field Type Select from one of the Java types available in the dropdown combo box. The Java type specifies the type of the variable.

Bean PK Check the options for the beans, which act as the primary key. There can be more than one primary key here.

After filling in all the information required in this panel, click Next. This takes the developer to the next panel called Define Container Managed Relationships.

Defining Container Managed Fields (without database)

If the option Use Database is un-checked in the Bean Definition panel, then this is the next panel to come up. This panel requires the developer to fill information about the table (the table is created at a later point) to be used by the entity bean.

This information is used to fill the deployment information like OR-Mapping and keeps the module (holding the bean) in a "ready-to-deploy" state, provided the table is created before deployment.

Follow the given steps to define the bean's CMP fields and additional information about the table fields, not yet created:

Table Name Enter the name of the table in this field.

Add Columns Once the name of the table is added, click on the + (plus symbol) button. This adds a column to the table.

DB Field Enter the name of the database field in this panel.

DB Type Select from the database types available in the dropdown combo box. The DB type defines the types of data that the column holds.

Bean Field Name Enter the bean field name here.

Bean Field Type Select from one of the Java types available in the dropdown combo box.

Bean PK Check the options for the beans, which act as the primary key. There can be more than one primary key here.

To add more columns in the table, click on + as many times. To delete columns that have been already defines, select it and click on -. After completing the table definition, click Next. This takes the developer to the next panel called Define Container Managed Relationships.

Defining Container Managed Relationships

This panel is used to define relationships between beans existing in the same module. Studio supports self relationships (bean having a relationship with itself). The panel therefore appears even when the bean being created is the first to be created in the module.

Note: In case of Self bi-directional relationships, Pramati Server supports only "One-to-many" relationships.

Each ejb-relationship-role describes a relationship role: its role source name, its multiplicity within a relation, and its navigability.

Follow the given steps to define the relationships:

Role 1 This section comprises the details of the bean being created for which the relationships have to be defined.

The user can add the CMR Field after the bean code is generated. Open the bean source and add a pair of get/set methods with their appropriate return types. Example: java.util.Collection or any Local interface of a bean in the same EJB Module. Once these are added, compile the bean and open it in the Bean Properties file, and map the relation, multiplicity, and cascade delete options.

Relation This section is where the developer describes the nature of the relationship between two roles.

Role 2 This section comprises the details of all the beans existing in the module, which are going to be involved in the relationships defined for the bean being created.

Use the Add button once all the details have been filled for the relationship being defined. Adding the relationship adds the relationship and displays the following details in the bottom half of the panel:

Bean A The EJB name of the bean (Role 1) for which the relationship is being defined is displayed here. This is not editable.

Relation The multiplicity-navigability icon of the relationship is displayed here. This is not editable.

Bean B The EJB name of the second bean (Role 2) in the relationship is displayed here. This is not editable.

Join Click on the symbol appearing in this field and a new panel called Join Condition pops up. When a Join condition has not been defined, the icon contains a question mark. Once the Join condition has been defined, the icon contains a green tick.

Selecting the added relationship, populates the Role 1 and ROle 2 details. Modify these changes and add them, after deleting the previous relationship.

JOIN Condition

Use this panel to define the JOIN Conditions between two tables in a relationship.

This panel is divided into three different sections. The first section is not editable, and displays the relationship details for which the JOIN condition is being defined. The Status changes to Resolved after the first JOIN Condition has been added.

The second section is where the condition is defined. Select the Table name and the Table Fields under LHS and RHS.

The Schema Filter field is used to filter the tables depending on the schema names in all the databases. When the database in use is Oracle, the schema name provided in the drop down combo box is ignored and the "User name" used for creating the resource is used as schema name. The Table Filter field is used to filter and display the tables in the Tables field. After specifying the filters, click on Get. This displays the results in the fields, Table Name and Table Fields.

Clicking on Add adds the conditions defined in the third section, which displays the JOIN Conditions. The conditions can be joined together using either OR, or AND. Click on the field to toggle between AND and OR.

Click OK to return to the Define Container Managed Relationships panel.Clicking Next in this panel takes you to the next panel called Create Methods Page.

Note: For deploying entity beans with container managed relationships on Pramati Server, ensure that there are database constraints between the tables, which they map to.

Creating Methods Page

This panel allows creation of ejbCreate Methods for the CMP 2.0 entity bean. The ejbCreate methods are responsible for creating and initializing the bean.

To create the ejbCreate methods using the Fields, do the following:

Choose CMFs Select the Container managed Fields from the list available in the Choose CMFs section.

Selected CMFs Click on the forward arrow icon. This moves the fields from the Choose CMFs section to the Selected CMFs section. To move the fields from the Selected CMFs section back to the Choose CMFs section, use the back arrow icon. Use the up and down arrow buttons to change the order of the fields.

Add EJB Create Methods Click on + in the panel and the fields get added to the EJB Create Methods section. This creates the ejbCreate Method with the parameters as the fields from the Selected CMFs section.

This process can be repeated to define all the methods required. To delete the methods from the EJB Create Methods section, select the method and click on -

Example

If balance and name are the fields as a parameter, the method that gets defined is ejbCreate(java.lang.string name, java.math.BigDecimal balance).

Clicking Next takes you to the next panel called Define Home Methods.

Defining Home Methods

This panel helps in creating the skeleton code for the EJB Home methods. These methods correspond to the home business methods specified in the bean's home interface.

To create the skeleton EJB Home methods, follow the given steps:

Method Name Enter the name of the Home method that is to be created. The method name must have ejbHome as its prefix followed by the method name where the first character has been uppercased.

Return Type Enter the return type of the method in this field. If the business method corresponds to a method of the entity bean's home and remote interface, then the return type value has to be the acceptable types for RMI-IIOP.

Parameters Enter the parameters with their respective Java types. Separate the parameters with commas.

Example

int ID, String Name

Exceptions Enter any arbitrary application specific exceptions here.

Example

No Large Accounts present.

Use + to add the home method. This displays the method added in the Methods section of the panel. To remove any method that has been added, select the method in the Methods section and click on -.

After entering all the information required for the Home Methods Page, click on Next. This takes you to the next panel called Define Business Methods.

Defining Business Methods

Use this panel to create skeleton code for business methods of the bean.

Method Name Enter the business method name here. The name can be anything, but should not start with the word ejb. This avoids conflicts with the callback methods used by the EJB architecture.

Return Type Enter the return types of the business method in this field. If the business method corresponds to a method of the entity bean's remote interface, then the return type value has to be the acceptable types for RMI-IIOP.

Parameters Enter the parameters with their respective return types. Separate the parameters with commas. If the business method corresponds to a method of the entity bean's remote interface, then the argument type value has to be the acceptable types for RMI-IIOP.

Exceptions Enter the exceptions that the business method needs to throw in this field.

Add the business method by clicking on +. This displays the method added in the Methods section of the panel. To remove any method that has been added, select the method in the Methods section and click on -.

After entering all the information required for the Business Methods Page, click on Next. This takes you to the next panel called Define Select Methods.

Defining Select Methods

This panel allows users to define the skeleton code of the ejbSelect methods for entity beans. These Select methods are helper methods that perform queries internally by the bean, but are not accessible to clients of the bean. Unlike finder methods described in the next section, Select methods are not specified in the entity bean's home interface, and remain only in the bean's code.

Key-in the following information to create the skeleton code for the Select methods:

Method Name Enter the name of the Select method that is to be created. Return Type Select from the Return types in the list available. Selecting Local Interface as the Return Type makes the return type as the current bean's local interface. Selecting Remote interface as the return type makes the return type of the Select method as the current bean's remote interface. Selecting it as CMP Field allows the user to select from any of the primitive types in the list in the Type field.

Parameters Enter any valid parameters preceded by the return type of the parameter.

Exceptions Enter the exceptions as any arbitrary application specific exceptions. The exception must define javax.ejb.FinderException.

Click on + to add the select method. The method gets added and the method name is always prefixed with the keyword ejbSelect. After the method is added, double click on the blank file icon under the Query field and add the EJB QL query in the Input Query panel that comes up. After entering the query, click OK. This changes the icon from an empty file to a coloured icon file, which shows the file contains the query. To know more on Query methods that use EJB QL, read the section Query Methods. Clicking Next takes you to the panel called Define Finder Methods.

Defining Finder Methods

Use this panel to define the skeleton code for the finder methods of CMP 2.0 beans. The finder methods created are always prefixed with ejbFind before the method name.

Key-in the following information to create the skeleton code for finder methods:

Method Name Enter the name of the Finder method that is to be created. Return Type Select from the two valid return types for finder methods - java.util.Collection and Local or Remote Interface.

Parameters Enter any valid parameters preceded by the Java type of the parameter.

Exceptions Enter the exceptions as any arbitrary application specific exceptions.

Click on + to add the finder method. The method gets added and the method name is always prefixed with the keyword ejbFind. After the method is added, double click on the blank file icon under the Query field and add the EJB QL query in the Input Query panel that comes up. After entering the query, click OK. This changes the icon from an empty file to a coloured icon file, which shows the file contains the query. To know more on Query methods that use EJB QL, read the section Query Methods. Clicking Next takes you to the panel called Select Bean Interfaces.

Query Methods

Entity beans with container-managed persistence and cmp-version 2.x, must use the EJB QL to specify a finder or select query for the entity bean.

EJB QL is used for two types of query methods:

findByPrimary key is generated by default, and therefore the user need not add the method.

Defining Bean Interfaces

This panel assists in defining local and remote interfaces for the bean. The EJB name and the bean class shows the current bean's EJB name and Bean Class name, and cannot be modified.

EJB Name This displays the EJB Name of the bean being crated. This field cannot be edited in this panel.

Bean Class This fields displays the class of the bean being created and is a non-editable field.

Remote Interfaces A remote interface defines the business methods that are callable by remote clients. Check this checkbox if the remote interfaces are to be created along with the bean.

Local Interfaces A local client can access an entity object through the entity bean's local interface. A local interface defines the business methods that are callable by local clients. Check this checkbox if the local interfaces are to be created along with the bean.

All the generated interfaces extend their javax.ejb.xxx interface as defined by the J2EE specification. Therefore the user need not specify javax.ejb.xxx in the Adv panel.

After entering all the information required, click Next. This displays the next panel called Methods to publish in Interface(s).

Methods to be published in Interface(s)- Home and Local Home

Check the home and local home interfaces for the finder methods created previously. These methods are then placed in the home interface. Click Next.

Methods to be published in Interface(s) - Remote and Local

This panel displays all the get and set methods of the CMFs and CMRs, and also the business methods that were defined earlier. Check the remote and local interfaces for those methods that are to be placed in the local interface. Click Finish.

The setXXX() methods for the primary key fields is not displayed in this panel. This method is also not available for selection in the Interface Wizard.

The wizard then generates the skeleton code for the CMP 2.0 bean as well as the business method. The Local and Remote Interfaces for the bean are also generated.


Pramati Technologies  © Copyright   TOCPREVNEXTINDEX