5.1 Mapping Approaches
Several approaches can be used to establish a mapping between your
persistent Java classes and a relational schema:
- Generate a relational schema from your persistent Java classes
-
If you are developing a new application in Java and you do not have
an existing relational database schema, you can let the JDO
implementation generate a relational schema from your object model.
This approach is commonly called forward
engineering the model. This approach yields a
high level of development productivity because all of the schema
design and mapping work is done automatically by the JDO
implementation. The JDO specification does not require support for
the automatic generation of a schema. Some implementations do not
support this approach and require you to define the mapping to an
existing schema. Many of the implementations that do support schema
generation let you specify some metadata to help direct the
algorithms generating the schema.
- Generate your persistent Java classes from a relational schema
-
In many cases, you may already be using a relational database schema
and you would like to write a new application with an object view of
the data. In this scenario, many implementations provide tools you
can use that analyze your relational schema and generate a Java
object model for you. This approach lets you develop an
object-oriented Java application quickly. It is commonly called
reverse-engineering
the model.
- Define a mapping between Java classes and a relational schema
-
You may have an existing relational schema and a separately designed
object model and you would like to define a mapping between the two.
In this case, you can use metadata directives to define how a class
and its fields should be mapped to the underlying datastore. This
approach is commonly called a bridge mapping
between the two models.
If you are using JDO with a relational database, JDO does not
preclude you from having some applications access the datastore with
JDBC and others access it with JDO. This capability allows you to
migrate to JDO gradually from a suite of JDBC-based applications. If
you have an existing relational schema, you will likely use
reverse-engineering or a bridge mapping. If you access the relational
database with JDO and JDBC, it becomes more important to understand
how the object model is mapped to the relational schema and follow
any rules the implementation may have about accessing the additional
columns and tables it requires.
Once you have developed a JDO application with an object model and
associated datastore, the object model and the datastore schema will
likely evolve as the needs of your application evolve. The JDO
metadata can be used to deal with this evolution of the two data
models. JDO does not define any specific support for
datastore-schema evolution,
object-model evolution, or the
associated aspects of evolving the two distinct data models. Support
for these is implementation-specific.
|