Mapping enterprise beans to database tables

Mapping enterprise bean JAR files (EJB modules) to relational database (RDB) tables enables the EJB modules to access database resources.

 

Before you begin

This article assumes that you have created an enterprise application that has an EJB module and that you want this application to access one or more resources.

This article also assumes that you have started an assembly tool such as the Application Server Toolkit (AST) or Rational Web Developer and have configured the assembly tool for work on J2EE modules.

 

Overview

We can map enterprise bean JAR files (EJB modules) to relational database (RDB) tables using the EJB to RDB Mapping wizard of an assembly tool. The wizard creates EJB to RDB mappings for the following situations:

Existing enterprise bean but no database schema

Top Down mapping generates a default database schema and a mapping from one or more existing enterprise beans.

Existing database schema but no enterprise bean

Bottom Up mapping generates one or more enterprise beans and mappings from an existing database schema.

Existing enterprise bean and database schema

Meet In the Middle mapping matches existing enterprise beans with existing database tables. We can match by name, by name and type, or by neither.

Top-down and meet-in-the-middle mapping support multiple backends, making multiple deployments inside a single EJB module configurable at run time. 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.0 beans). This mapping is used at run time when the JAR is installed on WebSphere Application Server. When deploying EJB 1.1 beans inside an EJB 2.0 project, the EJB 1.1 beans are deployed only once, using the first declared database and type. You specify a Backend ID in an EJB deployment descriptor editor under WebSphere Bindings. The Backend ID determines the persister classes that get loaded at deployment.

 

Procedure

  1. In the Project Explorer view, right-click the EJB module.

  2. Click EJB to RDB Mapping > Generate Map.

  3. After the wizard opens, press F1 and select a type of mapping. The online help provides detailed information on generating a mapping.

  4. For EJB 2.0 projects, on the EJB to RGB Mapping page specify whether you want to create a new backend (Top Down) or use an existing backend (Bottom Up or Meet In the Middle) where the schema exists in the backend but without a mapping file. If you previously generated a mapping, one can create and map unmapped elements or open the mapping editor to manually make changes. In EJB 2.0, your mapping and schema files make up a backend for EJB 2.0 projects. We can have multiple backend folders for each project; for example, one DB2 and one Oracle backend. The wizard uses one database backend only as the default, but one can define as many as we need.

  5. Follow the instructions in the wizard and in the online help.

  6. Click Finish to generate the mapping.

 

Result

Files for the updated module are shown in the Project Explorer view.

 

What to do next

After testing your module, one can deploy your module to an application server.

For EJB modules, one can generate EJB deployment code and deploy the module to a target server in one step. In the Project Explorer view, right-click on the EJB project and click Deploy.

 

See also


Mapping constraints for databases

 

Related Tasks


Assembling applications