JPA : Schema Mapping

You saw in our basic class mapping guide how you define a classes
basic persistence, notating which fields are persisted. The next step is to define how it maps to the
schema of the datastore (in this case RDBMS). The simplest way of mapping is to map each class to its own table.
This is the default model in JDO persistence (with the exception of inheritance).
If you don't specify the table and column names, then DataNucleus will generate table and column names for you.
You should specify your table and column names if you have an existing schema. Failure to do so will mean
that DataNucleus uses its own names and these will almost certainly not match what you have in the datastore.

Tables and Column names

The main thing that developers want to do when they set up the persistence of their data is to control the names of the
tables and columns used for storing the classes and fields. This is an essential step when mapping to an existing schema,
because it is necessary to map the classes onto the existing database entities. Let's take an example

So we have defined the table and the column names.
It should be mentioned that if you don't specify the table and column names then DataNucleus will generate
names for the datastore identifiers consistent with the JPA specification.
The table name will be based on the class name, and the column names will be based on the field names and
the role of the field (if part of a relationship).

Column nullability and default values

So we've seen how to specify the basic structure of a table, naming the table and its columns, and how to control the
types of the columns. We can extend this further to control whether the columns are allowed to contain nulls. Let's take a
related class for our hotel. Here we have a class to model the payments made to the hotel.

In this class we can model payments from a customer of an amount. Where the customer pays by bank transfer we can save the
reference number. Since the bank transfer reference is optional we want that column to be nullable. So let's
specify the MetaData for the class.

So we make use of the nullable attribute. The table, when created by DataNucleus, will then provide the nullability
that we require. Unfortunately with JPA there is no way to specify a default value for a field when it hasnt been
set (unlike JDO where you can do that).

Column types

DataNucleus will provide a default type for any columns that it creates, but it will allow users to
override this default. The default that DataNucleus chooses is always based on the Java type for the
field being mapped. For example a Java field of type "int" will be mapped to a column type of INTEGER
in RDBMS datastores. Similarly String will be mapped to VARCHAR. JPA does NOT allow detailed control
over the JDBC type as such, with the exception of distinguishing BLOB/CLOB/TIME/TIMESTAMP types.
Fortunately DataNucleus (from v3.0.2) provides an extension to overcome this flaw in the JPA spec.
Here we make use of a DataNucleus annotation @JdbcType

public class Payment
{
@JdbcType("CHAR")
String currency;
...
}

So we defined the JDBC type that this field will use (rather than the default of VARCHAR).

JPA does allow permit control over the length/precision/scale of columns. So we define this as follows

columnposition

With some datastores it is desirable to be able to specify the relative position of a column in the table schema.
The default (for DataNucleus) is just to put them in ascending alphabetical order.
DataNucleus allows an extension to JPA providing definition of this using the position of a column.
See fields/properties column positioning docs for details.