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

You have implemented the required access control methods.

Procedure

  1. 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%.
  2. Open the Data perspective and switch to the Data Definition view.
  3. Navigate to the following directory: WebSphereCommerceServerExtensionsData > ejbModule > META-INF.
  4. Select META-INF and from its pop-up menu, select New > Database Definition.
  5. In the Database name field, enter the name of your development database. There is no default database name. Apache DerbyThe 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.
  6. From the Database vendor type list, select one of the following types
    • Apache DerbyDB2
      • 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.
  7. Click Finish. If asked to confirm creation of the /WebSphereCommerceServerExtensionsData/ejbModule/META-INF/backends/ databaseType folder, click Yes.
  8. 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.
  9. Select databaseName and from its pop-up menu, select New > Schema Definition.
  10. 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.
  11. Right-click NULLID and select New > Table Definition.
  12. In the Table name field, type your table name and click Next.
  13. Add the columns to your table definition:
    1. 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.
    2. 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).
    3. If the field is in the primary key, select the Key column check box.
    4. Click Add again and repeat steps to add new fields for each column in the database table that requires a CMP field.
    5. 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 called optCounter of type short. Ensure you mark the optCounter field as the OptimisticPredicate in the Properties view. For example:
      Mark the optCounter field as the OptimisticPredicate in the Properties view
    6. Click Close to close this window.
  14. 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.
  15. Select Meet In The Middle and click Next.
  16. Select Match By Name and click Finish.
  17. In the Enterprise Beans pane, highlight the your entity bean. In the Tables pane, highlight your database table.
  18. 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.
  19. Proceed to modifying the schema name.