Mapping the database table to the new enterprise bean
After you create the new enterprise bean, you must create a mapping between the CMP fields in the bean and the columns in the database table. When both the enterprise bean and its corresponding database table exist, a "Meet-in-the-middle" type of mapping is used. Rational Application Developer provides tools to simplify this task.
Before you begin
Procedure
- Create a database connection by following the Connecting to a database topic in the IBM Rational Application Developer for WebSphere, v7.5.5 documentation . You should specify filtering options to load only the necessary database tables. For example, in the programming tutorials all new database tables begin with the letter X, so you can load only tables with names like X%.
- Open the Data perspective and switch to the Data Definition view.
- Navigate to the following directory: WebSphereCommerceServerExtensionsData > ejbModule > META-INF.
- Select META-INF and from its pop-up menu, select New > Database Definition.
- In the Database name field, enter
the name of your development database. There is no default database
name. The default database name is mall.
Note: The name you enter in the Database name field is not really used. When you create a database definition, you create XMI files that are later used to generate deployed code. These XMI files are independent of your database name.
- From the Database vendor type list,
select one of the following types
-
- DB2 Universal Database V8.2
- DB2 Universal Database Express V8.2
- DB2 Universal Database for iSeries V4R5
- Oracle 9i or Oracle 10g, depending on your target database.
-
- Click Finish. If asked to confirm creation of the /WebSphereCommerceServerExtensionsData/ejbModule/META-INF/backends/ databaseType folder, click Yes.
- Navigate to the following directory: WebSphereCommerceServerExtensionsData > ejbModule > META-INF > backends > databaseType > databaseName, where databaseType is the database type you chose in the previous step, and databaseName is the name of your development database.
- Select databaseName and from its pop-up menu, select New > Schema Definition.
- In the Schema name field, enter
NULLID
and click Finish. You use NULLID as the schema name because this allows flexibility for EJB beans to work against any schema. If the schema name is filled in with a specific value other than NULLID, the EJB bean would only work for tables created using a specific schema. - Right-click NULLID and select New > Table Definition.
- In the Table name field, type your table name and click Next.
- Add the columns to your table definition:
- In the Column name field, type an appropriate name for the new CMP field. Note that if you want to use the Match by name function later when mapping this field to its corresponding column in the database table, name your field exactly (case insensitive) to the name of the column.
- In the Column type field, type the appropriate data type for the field. Note that you should use the wrapper classes for primitive data types (for example, use the java.lang.Long data type, not the long data type).
- If the field is in the primary key, select the Key column check box.
- Click Add again and repeat steps to add new fields for each column in the database table that requires a CMP field.
- If you want this entity bean to use optimistic locking,
your database table must include a column called
optCounter
of type SMALLINT and its corresponding entity bean must have a field calledoptCounter
of typeshort.
Ensure you mark the optCounter field as the OptimisticPredicate in the Properties view. For example:
- Click Close to close this window.
- From the J2EE perspective, select WebSphereCommerceServerExtensionsData and from its pop-up menu, select EJB to RDB Mapping > Generate Map. The Use an existing backend folder radio button is pre-selected. Click Next.
- Select Meet In The Middle and click Next.
- Select Match By Name and click Finish.
- In the Enterprise Beans pane, highlight the your entity bean. In the Tables pane, highlight your database table.
- Map the fields in the your entity bean to the columns in the your database table by selecting Match By Name from the bean's pop-up menu.
- Proceed to modifying the schema name.