Java Reference
In-Depth Information
figure 4.1, which is a one-to-one relationship, is implemented by the
delivery-
Address
field, which references an
Address
object and the
PendingOrder
-
Restaurant
relationship, which is a many-to-one relationship, is implemented by a
restaurant
field, which references a
Restaurant
:
public class PendingOrder {
private Address deliveryAddress
private Restaurant restaurant;
…
The
PendingOrder.restaurant
field represents a many-to-one relationship
because multiple pending orders can be for the same restaurant. The
Pending-
Order.deliveryAddress
field represents either a one-to-one relationship where
each address belongs to a single
PendingOrder
or a many-to-one relationship
where each
Address
is shared by many
PendingOrder
s.
There are a couple of ways of mapping a one-to-one relationship. One option
is to use the Embedded Value pattern described earlier and map the child object
to the parent object's table. The other option is for the referenced class to have its
own table. For example, the delivery address for a
PendingOrder
can be stored in
its own table as follows:
CREATE TABLE PENDING_ORDER (
PENDING_ORDER_ID NUMBER(10),
DELIVERY_TIME DATE
…
)
CREATE TABLE DELIVERY_ADDRESS
PENDING_ORDER_ID NUMBER(10),
DELIVERY_STREET1 VARCHAR2(50),
DELIVERY_STREET2 VARCHAR2(50),
DELIVERY_CITY VARCHAR2(50),
DELIVERY_STATE VARCHAR2(2),
DELIVERY_ZIP VARCHAR2(10),
…
The delivery address is stored in the
DELIVERY_ADDRESS
table, whose primary key
is
PENDING_ORDER_ID
.
A many-to-one relationship is mapped by the referencing object's table having
a foreign key to the referenced object's table. For example, the
PendingOrder
-
Restaurant
relationship from figure 4.1 can be mapped by the
PENDING_ORDER
table having a foreign key to the
RESTAURANT
table:
CREATE TABLE PENDING_ORDER (
…
RESTAURANT_ID NUMBER(1),
Search WWH ::
Custom Search