Approaches for mapping enterprise beans to database tables

There are three general approaches for creating your initial mappings between enterprise beans and relational database tables: top-down, bottom-up, and meet-in-the-middle.

You can use the EJB mapping wizard in the EJB tools and use the following development approaches for mapping enterprise beans to database tables:

Top-down

This approach assumes that you already have existing enterprise beans. In this approach, the enterprise bean design determines the database design.

After you finish defining your enterprise beans, you can generate a schema and map.

A set of tables are generated to support the CMP entities inside the EJB project. In these tables, each column corresponds to a CMP field of the enterprise bean, and the generated mapping maps the field to the column. Relationships are mapped to foreign-key relationships.

By default, EJB inheritance hierarchies are mapped to a single table. In other words, the base and all derived enterprise beans are mapped to the same database table. Additional options exist that support generating joined tables for the leaf enterprise beans.

Note: If you do not manually map your beans, top-down maps are automatically generated when you generate deployment code using IBM DB2 Universal Database Version 8.1 as the default database vendor.

Bottom-up

This approach assumes that the database tables already exist, and that once the selected tables are imported, the enterprise beans and mappings between them are automatically generated. By default, relationships are generated where foreign-keys exist.

Meet-in-the-middle

In the meet-in-the-middle approach of mapping enterprise beans to database tables, it is assumed that you have existing enterprise beans and a database schema. Using the mapping editor, you can do a meet-in-the-middle mapping by matching by name, by type, or no matching.

In this approach, you map each field of the enterprise bean to the corresponding column of a table within the selected schema. Because roles have two ends, mapping one end (or one role) to a foreign-key, will automatically map the other end to the same foreign-key.

Additionally, if the source database or JAR file changes over time, the map and resulting tables or beans can keep up with these changes by "Re-executing" the mapping commands that are saved with the mapping document.

If there are any required maps missing, the task list will display an error for each missing map when the map file is saved, or validation is run on the project. This will prevent EJB deployment code generation from running until you fix the errors and complete the map.

Backends folder

This product supports multiple backends that make multiple deployments inside a single EJB module configurable at runtime. Both top-down and meet-in-the-middle mapping approaches support multiple backends. Bottom-up mapping only supports a single backend. A backend can represent different database vendors, or simply alternative mappings and table qualifiers. If multiple backends exist, then current BackendID needs to be set in the EJB deployment descriptor editor (when working with EJB 2.x beans). This mapping/deployment will be used at runtime when the JAR is installed on WebSphere Application Server, version 5.0. When deploying EJB 1.1 beans inside an EJB 2.x project, the EJB 1.1 beans are deployed only once, using the first declared database and type.

1 Bottom-up mapping is not available in