web server - Borland Technical Publications

web server - Borland Technical Publications web server - Borland Technical Publications

techpubs.borland.com
from techpubs.borland.com More from this publisher
12.11.2014 Views

Container-Managed Persistence in Borland Enterprise Server 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: LINE ORDER_NO ITEM QUANTITY COLOR SIZE 001 XXXXXXX01 Kitty Sweater 2 red XL 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. LINE QUANTITY COLOR SIZE 001 2 red XL Again, we can describe these relationships using a combination of elements and a element. The 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 element. Now let's look at the XML. First we define the CMP fields for the LineItem entity bean: LineItem . . orderNumber ORDER_NO line LINE item ITEM quantity QUANTITY.QUANTITY color QUANTITY.COLOR size QUANTITY.SIZE Chapter 15: Entity Beans and Table Mapping for CMP 2.0 135

Container-Managed Persistence in Borland Enterprise Server Next, we specify the linking column between LINE_ITEM and QUANTITY by using a element. LINE_ITEM LINE QUANTITY LINE CASE 1: a unidirectional one-toone relationship. Specifying relationships between tables To specify relationships between tables, you use the element in ejbborland.xml. Within the element, you define an containing the role's source (an entity bean) and a element containing the relationship. The descriptor then uses elements to specify relationships between two tables, a and a . You must observe the following cardinalities: ■ One must be defined per direction; if you have a bidirectional relationship, you must define an for each bean with each referencing the other. ■ Only one element is permitted per relationship. Within the and elements, you specify a column list that contains the column names to be linked together. The column list corresponds to the element in the descriptor. The XML is: Let's look at some relationships to see how this XML is put into practice: Here, we have a Customer entity bean with a primary key, CUSTOMER_NO, that is also used as a primary key for an entity called SpecialInfo, which contains special customer information stored in a separate table. We need to specify a relationship between these two entities. The Customer entity uses a field called specialInformation to map to the SpecialInfo bean. We specify two relationship roles, one for each bean and assign either to left- and/or right-table. Then we specify the name of their related column for both. Customer specialInformation CUSTOMER CUSTOMER_NO 136 BES Developer’s Guide

Container-Managed Persistence in <strong>Borland</strong> Enterprise Server<br />

Mapping CMP fields to multiple tables<br />

You may have an entity that contains information persisted in multiple tables. These<br />

tables must be linked by at least one column representing a foreign key in the linked<br />

table. For example, you might have a LineItem entity bean mapping to a table LINE_ITEM<br />

with a primary key LINE that is a foreign key in a table called QUANTITY. The LineItem<br />

entity also contains some fields from the QUANTITY table that correspond to LINE entries<br />

in LINE_ITEM. Here's what our LINE_ITEM table might look like:<br />

LINE ORDER_NO ITEM QUANTITY COLOR SIZE<br />

001 XXXXXXX01 Kitty Sweater 2 red XL<br />

QUANTITY, COLOR, and SIZE are all values that are also stored in the QUANITY<br />

table, shown here. Note the identical values for some of the fields. This is because the<br />

LINE_ITEM table itself stores information in the QUANTITY table, using the LineItem<br />

entity to provide composite information.<br />

LINE QUANTITY COLOR SIZE<br />

001 2 red XL<br />

Again, we can describe these relationships using a combination of <br />

elements and a element. The elements define the fields found<br />

in LineItem. Since there are some fields that require information from QUANTITY, we'll<br />

specify that generically by using a TABLE_NAME.COLUMN_NAME syntax. For instance, we'd<br />

define LINE_ITEM's COLOR column as QUANITY.COLOR. Finally, we'll specify the linking<br />

column, LINE, that makes up our primary key/foreign key relationship. We'll do this<br />

using the element.<br />

Now let's look at the XML. First we define the CMP fields for the LineItem entity bean:<br />

<br />

LineItem<br />

.<br />

.<br />

<br />

<br />

orderNumber<br />

ORDER_NO<br />

<br />

<br />

line<br />

LINE<br />

<br />

<br />

item<br />

ITEM<br />

<br />

<br />

quantity<br />

QUANTITY.QUANTITY<br />

<br />

<br />

color<br />

QUANTITY.COLOR<br />

<br />

<br />

size<br />

QUANTITY.SIZE<br />

<br />

Chapter 15: Entity Beans and Table Mapping for CMP 2.0 135

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!