As in persistence metadata, mapping metadata uses the class
element and its name
attribute to
identify persistent classes. Mapping metadata, however, expands the
class
element with the table
attribute.
The table
attribute specifies the name of the table
whose rows represent instances of this class. If the table is in a
non-default schema, you can specify this attribute as
<schema-name>.<table-name>
. This
attribute is optional, because not all classes must be mapped to unique
tables. We explore the mapping of classes to tables in more depth in
Section 15.8, “Inheritance”.
Sometimes, some of the fields in a class are mapped to secondary
tables. In that case, use the class' table
attribute to name what you consider the class' primary table. Later,
we will see how to map certain fields to other tables.
The example below maps classes to tables according to the following
diagram. Note that the abstract Contract
class
is left unmapped. The SUB
,
TRIAL_SUB
, and LINE_ITEM
tables are in the
CNTRCT
schema; all other tables are in the default
schema.
Note that the example does not include our model's
Address
class in the mapping metadata document.
In Chapter 5, Metadata, we defined
Address
as an embedded only class.
That means that Address
instances are only
stored as part of other records; therefore
Address
does not have its own mapping.
Example 15.2. Mapping Classes
<?xml version="1.0"?> <orm> <package name="org.mag"> <sequence name="ArticleSeq" datastore-sequence="ART_SEQ"/> <class name="Magazine" table="MAG"> ... </class> <class name="Article" table="ART"> ... </class> </package> <package name="org.mag.pub"> <sequence name="AuthorSeq" factory-class="Author$SequenceFactory"/> <class name="Company" table="COMP"> ... </class> <class name="Author" table="AUTH"> ... </class> </package> <package name="org.mag.subscribe"> <sequence name="ContractSeq" strategy="transactional"/> <class name="Contract"> ... </class> <class name="Subscription" table="CNTRCT.SUB"> ... </class> <class name="LifetimeSubscription"> ... </class> <class name="TrialSubscription" table="CNTRCT.TRIAL_SUB"> ... </class> <class name="Subscription$LineItem" table="CNTRCT.LINE_ITEM"> ... </class> </package> </orm>