Specifying Primary and Secondary Tables

See Also

When you map CMP beans to a database, most beans map directly to tables, and the CMP fields map to columns in either the primary or a secondary table. Once a bean is mapped to a primary table, you have the option of mapping one or more secondary tables.

Primary Tables

A primary table is the table that most closely matches the bean you are mapping. A primary table must be selected for each CMP bean.

Secondary Tables

A secondary table enables you to map fields in your bean directly to columns that are not part of your primary table. You might want to do this if, for example, the data that represents a logical object is split across two or more tables.

A secondary table differs from relationships. You use secondary tables when you want columns from another table to be fields in the same bean. You use relationship fields when there is a logical relationship between tables. For example, if you want the entire row of another table to be an object in a single bean, you would map that with a relationship.

A secondary table must be related to the primary table by one or more columns whose associated rows have the same values in both tables. This link is called a reference key. Normally, this key is a foreign key in the primary table.

Configuring Primary and Secondary Tables in the IDE

For the Sun Java System Application Server, you can configure primary and secondary tables in the Sun Configuration editor. If you are deploying to a different application server, you have to edit the server-specific deployment descriptors yourself.

To specify the primary and secondary tables for an entity bean:

  1. In the Projects window, expand the Configuration Files node for your EJB module and double-click sun-cmp-mappings.xml or sun-ejb-jar.xml.
  2. In Sun Configuration editor, expand the Sun Configuration node and select the entity bean you want to map.
  3. If necessary, specify a database schema for the bean:
    1. Click Advanced Settings.
    2. Select the schema from the Database Schema drop-down list. The drop-down list contains all of the database schema (.dbschema) files in the project. If a schema is already specified, choosing a new schema removes all of the current field mappings that refer to this schema.
  4. Select the primary table from the Primary Table drop-down list. The drop-down list displays all of the tables in the bean's database schema.
  5. If necessary, specify any secondary tables for the bean:
    1. Click Advanced Settings.
    2. Select a table to use as the secondary table from the combo box. The combo box lists the tables available in the schema. When you select a secondary table, the IDE checks for a foreign key between the primary and secondary tables. If a foreign key between the tables exists, it is displayed as the reference key by default. If the database schema has not defined a foreign key, you need to specify a reference key between the two tables.
    3. In the dialog box, select the column or columns from the primary and secondary tables you wish to use as the reference key.
See Also
Defining a CMP Field
Managing Container-Managed Relationships
Generating CMP Entity Beans from a Database
Obtaining a Database Schema
Mapping an Entity Bean to a Database
Mapping Relationship Fields

Legal Notices