Java Reference
In-Depth Information
The Address class could be mapped to its own table, as I describe a bit later, but it
is simpler and more efficient to map its fields to PENDING_ORDER :
CREATE TABLE PENDING_ORDER (
PENDING_ORDER_ID NUMBER(10),
STATE NUMBER(5)
DELIVERY_TIME DATE,
DELIVERY_STREET1 VARCHAR2(50),
DELIVERY_STREET2 VARCHAR2(50),
DELIVERY_CITY VARCHAR2(50),
DELIVERY_STATE VARCHAR2(2),
DELIVERY_ZIP VARCHAR2(10),
)
The PENDING_ORDER table has columns such as DELIVERY_STREET1 and
DELIVERY_CITY that store the delivery address fields.
This approach simplifies the database schema by reducing the number of
tables. It also improves performance by reducing the number of joins required to
retrieve data. For example, the application can retrieve a PendingOrder and its
delivery address by only querying the PENDING_ORDER table.
Map a class to multiple tables
We will usually map a class to either its own table or to its parent's table. But some-
times, you need to map a class to multiple tables. This is useful when you're map-
ping an object model to a legacy schema. It can also be used to improve
performance when a class has a large number of attributes. Instead of mapping the
class to a single table with a large number of columns, the less frequently used
attributes can be mapped to a separate table, which is queried only when necessary.
Now that we have explored how to map classes and their simple fields to the
database, let's look at mapping relationships.
4.1.2
Mapping object relationships
We have seen that simple fields are easily mapped to table columns. However,
mapping the other fields that represent relationships between objects is a little
more complicated. There are several kinds of relationships between objects,
including one-to-one, many-to-one, one-to-many, and many-to-many. Let's see how
to map each one.
Mapping one-to-one and many-to-one relationships
One-to-one and many-to-one relationships are implemented by fields that refer-
ence the other object. For example, the PendingOrder - Address relationship from
 
 
 
Search WWH ::




Custom Search