user manual

Chapter 15: Entity Beans and Table Mapping for CMP 2.0 159
Container-Managed Persistence in Borland Enterprise Server
<cmp-field-map>
<field-name>Address.AddressState</field-name>
<column-name>STATE</column-name>
</cmp-field-map>
<cmp-field-map>
<field-name>Address.AddressZip</field-name>
<column-name>ZIP</column-name>
</cmp-field-map>
</cmp-field>
</cmp2-info>
.
.
</entity>
Note that we use one <cmp-field-map> element per database column.
Mapping CMP fields to multiple tables
You may have an entity that contains information persisted in multiple tables.
These tables must be linked by at least one column representing a foreign key
in the linked table. For example, you might have a LineItem entity bean
mapping to a table LINE_ITEM with a primary key LINE that is a foreign key in a
table called QUANTITY. The LineItem entity also contains some fields from the
QUANTITY table that correspond to LINE entries in LINE_ITEM. Here's what our
LINE_ITEM table might look like:
QUANTITY, COLOR, and SIZE are all values that are also stored in the
QUANITY table, shown here. Note the identical values for some of the fields.
This is because the LINE_ITEM table itself stores information in the
QUANTITY table, using the LineItem entity to provide composite information.
Again, we can describe these relationships using a combination of <cmp-field>
elements and a <table-ref> element. The <cmp-field> elements define the
fields found in LineItem. Since there are some fields that require information
from QUANTITY, we'll specify that generically by using a TABLE_NAME.COLUMN_NAME
syntax. For instance, we'd define LINE_ITEM's COLOR column as QUANITY.COLOR.
Finally, we'll specify the linking column, LINE, that makes up our primary key/
foreign key relationship. We'll do this using the <table-ref> element.
Now let's look at the XML. First we define the CMP fields for the LineItem entity
bean:
<entity>
<ejb-name>LineItem</ejb-name>
LINE ORDER_NO ITEM QUANTITY COLOR SIZE
001 XXXXXXX0
1
Kitty
Sweater
2redXL
LINE QUANTITY COLOR SIZE
001 2 red XL