7.5. Mapping Factory

7.5.1. Importing and Exporting Mapping Data

An important decision in the object-relational mapping process is how and where to store the data necessary to map your persistent classes to the database schema.

Chapter 12, Mapping Metadata in the JPA Overview describes JPA mapping options.

In JDO, mapping metadata is integrated with persistence metadata in your .jdo files by default. Section 15.1, “Mapping Metadata Placement” in the JDO Overview explains how to use separate .orm files for mapping metadata instead. Using separate files separates concerns and allows you to define multiple mappings for the same object model, but it is slightly less convenient to work with.

Section 6.2, “Metadata Factory” introduced Kodo's MetaDataFactory interface. Kodo uses this same interface to abstract the storage and retrieval of mapping information. Kodo includes the built-in mapping factories below, and you can create your own factory if you have custom needs. You control which mapping factory Kodo uses with the kodo.jdbc.MappingFactory configuration property.

Kodo allows you to mix metadata and mapping factories from both the JPA and JDO specifications. For example, you can configure Kodo to use JPA annotations for metadata but JDO .orm files for mapping information. We present an example of this configuration below.

The bundled mapping factories are:

Example 7.19. Standard JPA Configuration

In the standard JPA configuration, the mapping factory is left unset.

<property name="kodo.MetaDataFactory" value="jpa"/>

Example 7.20. Standard JDO Configuration

In the standard JDO configuration, the mapping factory is left unset.

kodo.MetaDataFactory: jdo

Example 7.21. Recording Constraint Names

This example uses standard JDO mapping, but uses the MappingFactory property to tell Kodo to record the names of all generated constraints in the mappings.

kodo.jdbc.MappingFactory: ConstraintNames=true

Example 7.22. JDO ORM File Configuration

You can configure Kodo to separate JDO mapping data into .orm files either by setting the kodo.Mapping property to the logical mapping name, or by explicitly setting your kodo.jdbc.MappingFactory property. unset.

kodo.MetaDataFactory: jdo
kodo.Mapping: oracle
kodo.MetaDataFactory: jdo
kodo.jdbc.MappingFactory: jdo-orm(Mapping=oracle)

Example 7.23. Storing JDO Mappings in a Table

This example stores JDO mapping metadata in the MAPPING database table.

kodo.jdbc.MappingFactory: jdo-table(Table=MAPPING)

Example 7.24. JPA Metadata, JDO Mapping Files

This example configures Kodo to use JPA annotations for metadata, but JDO .orm files for mappings.

<property name="kodo.MetaDataFactory" value="jpa"/>
<property name="kodo.jdbc.MappingFactory" value="jdo-orm"/>

7.5.1. Importing and Exporting Mapping Data

The mapping tool has the ability to import mapping data into the mapping factory, and to export mapping data from the mapping factory. Importing and exporting mapping data is useful for a couple of reasons. First, you may want to use a mapping factory that stores mapping data in an out-of-the-way location like the database, but you still want the ability to manipulate this information occasionally by hand. You can do so by exporting the data to XML, modifying it, and then re-importing it.

Example 7.25. Modifying Difficult-to-Access Mapping Data

mappingtool -a export -f mappings.xml package.jdo
... modify mappings.xml file as necessary ...
mappingtool -a import mappings.xml

Second, you can use the export/import facilities to switch mapping factories at any time.

Example 7.26. Switching Mapping Factories

mappingtool -a export -f mappings.xml *.jdo
... switch the kodo.jdbc.MappingFactory configuration ...
... property to list your new mapping factory choice  ...
mappingtool -a import mappings.xml

Kodo uses JDO's orm mapping format for imports and exports. See Chapter 15, Mapping Metadata for details on the orm mapping format.

 

Skip navigation bar   Back to Top