Java Reference
In-Depth Information
CONSTRAINT P_ORDER_RESTAURANT_FK
FOREIGN KEY(RESTAURANT_ID)
REFERENCES RESTAURANT(RESTAURANT_ID),
)
CREATE TABLE RESTAURANT (
RESTAURANT_ID NUMBER PRIMARY KEY,
)
In this example, the Restaurant class is mapped to the RESTAURANT table, whose
primary key is RESTAURANT_ID . The restaurant field of the PendingOrder class is
mapped to the RESTAURANT_ID column of the PENDING_ORDER table. It is a for-
eign key to the RESTAURANT table.
Mapping one-to-many relationships
Java classes don't just have fields that store simple values and references to other
objects. They also have collection fields such as lists, maps, sets, and fields that
store arrays. These collection and array fields implement one-to-many and many-
to-many relationships. A relationship called A-B is one-to-many when each B
object is only referenced by a single A object. The Food to Go domain model con-
tains several examples of one-to-many relationships. For instance, the Pending-
Order.lineItems field implements the PendingOrder - PendingOrderLineItem
relationship in figure 4.1, which is a one-to-many relationship:
public class PendingOrder {
private List lineItems; /* List<PendingOrderLineItem> */
}
A one-to-many relationship is usually mapped using a foreign key in the refer-
enced class's table. We can, for instance, map the PendingOrder lineItems field
using a foreign key in the PENDING_ORDER_LINE_ITEM table:
CREATE TABLE PENDING_ORDER_LINE_ITEM (
PENDING_ORDER_ID NUMBER(10)
LINE_ITEM_INDEX NUMBER(10) NOT NULL,
CONSTRAINT P_ORD_LINE_ITEM_ORDER_FK
FOREIGN KEY(PENDING_ORDER_ID)
REFERENCES PENDING_ORDER(PENDING_ORDER_ID)
The PENDING_ORDER_LINE_ITEM table has a PENDING_ORDER_ID column, which is
a foreign key to the PENDING_ORDER table. In addition, because lists are ordered,
 
Search WWH ::




Custom Search