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.
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
public class Hotel
private String name;
private String address;
private String telephoneNumber;
private int numberOfRooms;
In our case we want to map this class to a table called
, and has columns
(amongst other things). So
we define our Meta-Data like this
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).
See also :-
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.
public class Payment
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.
<column name="TRANSFER_REF" nullable="true"/>
<column name="CURRENCY" default-value="GBP"/>
So we make use of the
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).
See also :-
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. The thing it does allow is permit control over the length/precision/scale of columns
to we define this as follows
<column name="TRANSFER_REF" nullable="true" length="255"/>
<column name="CURRENCY" default-value="GBP" length="3"/>
<column name="AMOUNT" precision="10" scale="2"/>
So we have defined TRANSFER_REF to use VARCHAR(255) column type, CURRENCY to use (VAR)CHAR(3) column type, and AMOUNT to use
DECIMAL(10,2) column type.
See also :-