Creating Entity Beans with the Top-Down Approach


Creating an entity bean from the top down is comprised of the following general steps:

1.

Add a bean to your EJB module, using the Entity Bean template in the New File wizard.

2.

Select a primary key class for the bean, using the ejb-jar.xml file's visual editor.

3.

(For BMP beans) Implement the life-cycle methods.

4.

(For CMP beans) Add container-managed fields. You can generate such fields by right-clicking the entity bean's "logical" node (under the Enterprise Beans node) in the Projects window and choosing Add | CMP Field, or by right-clicking in the bean class in the Source Editor and choosing EJB Methods | AddCMP Field.

5.

(For CMP beans) Add container-managed relationships. You can do so in the ejb-jar.xml file's visual editor.

6.

Add finder methods. You can generate such methods by right-clicking a bean class in the Projects window and choosing Add | Finder Method, or by right-clicking the bean class in the Source Editor and choosing EJB Methods | Add Finder Method.

7.

Add business methods. You can generate such methods by right-clicking a bean class in the Projects window and choosing Add | Business Method, or by right-clicking the bean class in the Source Editor and choosing EJB Methods | Add Business Method.

8.

Set up the data source on the application server.

9.

(CMP only) If the application server's CMP capability is being used, map the abstract schema specified in the entity bean to a persistence model.

Creating an Entity Bean

NetBeans IDE provides a wizard for creating entity beans in an EJB project (see Figure 13-1).

Figure 13-1. New File wizard with the Entity Bean template selected


The wizard collects the minimum information necessary for creation of either a CMP or BMP entity bean. The wizard generates the necessary deployment descriptor entries along with a local home, local component interface (this provides the capability to perform compile-time checking on the bean class for the component interface methods), business interface, and a bean class.

The classes are named following the Java BluePrints recommendations. The component interface will initially be empty, and the home interface will contain the required findByPrimaryKey method. The primary key will default to String, which can be changed later using the visual editor (which is accessible by doubleclicking the ejb-jar.xml file, and can be found under the Configuration Files node in the Projects window). The visual editor for the deployment descriptor is shown in Figure 13-2.

Figure 13-2. Visual editor for a CMP bean deployment descriptor


The deployment descriptor entry includes a display name (using Java BluePrints recommended naming conventions) based on the EJB name. The assembly descriptor also is updated to require transactions for all methods.

Selecting a Primary Key Class

You can select a primary key class for the bean in the deployment descriptor (ejb-jar.xml file). A Java class representing the unique identifier (key) for each entity type must be selected. A Java class must represent the entire key. Compound keys and fields defined as primitive types require creation of wrapper classes. The wrapper class generally overrides equals and hashCode. For CMP beans, this class must also provide public fields with names matching the CMP fields composing the key.

Implementing Life-Cycle Methods

For BMP beans, you need to implement the life-cycle methods. The required work will also be highlighted using TODO comments in the code generated by the bean template.

Typically, a relational database is used for persistence, so this may require configuration (see Consuming Java Enterprise Resources later in this chapter for more information). The ejbStore method must persist the data model from memory to the persistent storage (SQL update command). The ejbLoad method must retrieve data from the persistent model into the Java data model (SQL select statement). The ejbRemove method must remove data from the persistent model (SQL delete statement). Although not required, an ejbCreate statement can be used to add to persistent storage (SQL create statement). The ejbFindByPrimaryKey method is also required, which should determine whether the key is in persistent storage.

Adding Container-Managed Fields

For CMP beans, you need to add container-managed fields. Container-managed fields are the data attributed to this entity. Fields can be added via the EJB Methods menu in the Source Editor, the Add submenu of the contextual menu of the bean's node in the Projects window, or the visual editor for the ejb-jar.xml file. The wizards also provide checkboxes to expose the methods in the local interface.

Adding Container-Managed Relationships

(CMP only) To add container-managed relationships, select the CMP Relationships view in the ejb-jar.xml file's editor and then click the Add button to open the Add CMP Relationship dialog box (shown in Figure 13-3). A containermanaged relationship represents an association between entity beans. The relationship is managed by the container, which means that adding or removing from one side of the relationship is reflected in the other side.

Figure 13-3. Add CMP Relationship dialog box


The Add CMP Relationship dialog box collects the information necessary to generate the necessary deployment descriptor entries and the abstract get and set methods (if navigable).

In the Add CMP Relationship dialog box, the Entity Bean combo box represents the bean on each side of the relationship. The Cascade Delete checkbox enforces referential integrity by removing the dependent bean if the referenced bean is removed. A container-managed relationship (CMR) field (represented as an abstract getter and setter in the bean class) provides the capability to navigate to the other side of the relationship. These fields can be exposed using the Add to Local Interface checkbox.

Adding Finder Methods

Finder methods represent the capability to obtain a set of entity beans based on a set of search criteria. The Add Finder method dialog box can be accessed by rightclicking a bean class in the Projects window (and choosing Add | Finder Method) or the Source Editor (and choosing EJB Methods | Add Finder Method).

The EJB specification requires that a findByPrimaryKey method be present. CMP beans specify an EJB Query Language (EJBQL) query to define additional finder methods. The EJBQL query must return instances of the abstract schema for this EJB. The default abstract schema name is the enterprise bean name, so a typical bean query would be SELECT OBJECT(o) FROM MyAbstractSchemaName AS o WHERE o.color = ?1. BMP beans need to return the primary key (or keys) that meet the search criteria.

Adding Business Methods

Business methods provide the capability to expose services. Entity beans may add business methods to provide additional logic or to expose data. Adding business methods may be done using the EJB Methods menu from the Source Editor contextual menu or from the Add submenu of the contextual menu for the enterprise bean's node in the Projects window.

Setting up a Data Source on the Application Server

A data source must be set up on the application server. This applies to both BMP and CMP beans. BMP beans may be able to take advantage of the IDE's Use Database enterprise resource capability, whereas CMP beans generally must specify a reference to the application server's CMP resource. (The form varies among application servers but typically is similar to what is done to set up a standard JDBC data source.)

Mapping a Bean's Schema to a Persistence Model

If the application server's CMP capability is being used, the mapping from the abstract schema specified in the CMP entity bean to a persistence model (most commonly a relational database) must be performed. The Sun Java System Application Server provides the capability to create the tables during deployment (so no mapping is necessary in this case). This can be done by specifying the Create Table At Deploy (and, optionally, Drop Table at Undeploy) properties in the IDE's visual editor for the project's sun-ejb-jar.xml file, as shown in Figure 13-4.

Figure 13-4. Visual editor for the sun-ejb-jar.xml file (Sun Configuration panel)


Mapping is done by describing how each CMP field and relationship represents a column in the database. As of the EJB 2.1 specification, this mapping is server specific, and thus must be performed using the application server editor. When performing the mapping using Sun Java System Application Server (versions 8.1 and 8.2), the first step is to create a database schema file from an existing database. The database schema must be created as a peer to the sun-ejb-jar.xml file (which is located in the src/conf folder and can be accessed from the Configuration Files node in the Projects window). A database schema object provides an XML snapshot of the database metadata. The application server uses this during design-time mapping as a way to access database metadata (without requiring a live database connection). This file is also passed to the application server during deployment for use during code generation.

Once the database schema object has been created, each CMP bean must specify the database schema that contains the table to which it is mapping. Double-click the node for the sun-ejb-jar.xml file to open its visual editor. In the visual editor, select the node for the entity bean in the left pane, select the Cmp Mapping tab, and click the Advanced Settings button. The visual editor with the Cmp Mapping tab selected is shown in Figure 13-5. The Advanced Settings dialog box is shown in Figure 13-6.

Figure 13-5. Visual editor for the sun-ejb-jar.xml file (EJB panel with Cmp Mapping tab selected)


Figure 13-6. CMP Mapping Advanced Settings dialog box


The final step of mapping CMP and relationship fields from a table can be performed from the Cmp Mapping tab (see Figure 13-7).

Figure 13-7. CMP mapping in the sun-ejb-jar.xml visual editor


The Automap All button automates the mapping between tables and fields. Using this feature provides full automation for most mappings, which can be further customized later (or even removed using the Unmap All feature).



NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net