Java Reference
In-Depth Information
Map a class to its own table
The simplest approach is to define a table for the class and map the class's simple
fields (e.g., those of type int , String , and Date ) to table columns. For example,
the PendingOrder class from the Food to Go application has fields such as state
and deliveryTime :
public class PendingOrder {
private int state;
private Date deliveryTime;
}
This class and its simple fields can be mapped to the PENDING_ORDER table:
CREATE TABLE PENDING_ORDER (
PENDING_ORDER_ID NUMBER(10),
STATE NUMBER(5)
DELIVERY_TIME DATE,
)
The PendingOrder class is mapped to the PENDING_ORDER table, whose primary
key is PENDING_ORDER_ID . The class's simple fields are mapped to columns of this
table. For example, the deliveryTime field maps to the DELIVERY_TIME column.
Map a class to its parent's table
Another way to map a class to a database schema is to map it to some other class's
table. This approach, also called the Embedded Value pattern [Fowler 2002], is
often used to persist a simple value object that is a child of a parent object. The
fields of the child are mapped to the columns of the parent object's table. For
example, the PendingOrder class has a deliveryAddress field, which is a reference
to an Address object:
public class PendingOrder {
private Address deliveryAddress;
}
public class Address {
private String street1;
private String street2;
private String city;
private String state;
private String zip;
}
 
 
Search WWH ::




Custom Search