7.3. Mapping Factory

7.3.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. If you rely on the mapping tool to do all your mapping for you, you might want to keep mapping data out of the way in a database table. On the other hand, if you want easy access to your mapping information, or if you do not want to store any additional metadata in your database, you might want to store it as vendor extensions in your JDO metadata. Or perhaps JDO's metadata extension mechanism is too verbose for your tastes, and you'd like to use separate, more concise mapping files to express your mappings.

Kodo JDO uses the kodo.jdbc.meta.MappingFactory interface to abstract the storage and retrieval of mapping information. Kodo JDO includes built-in mapping factories for all of the options mentioned above, and you can create your own factory if you have custom needs. You control which mapping factory Kodo JDO uses with the kodo.jdbc.MappingFactory configuration property.

The bundled mapping factories are:

Note that using a mapping factory other than the MetaDataMappingFactory does not obviate the need for JDO metadata extensions. Extensions such as dependent, inverse-owner, and, if you use Kodo to create your schema, jdbc-size and jdbc-indexed still reside in your JDO metadata. While some of these extensions may affect object/relational mapping behavior, they do not contain object/relational mapping data per se. Mapping factories only hold information directly related to object/relational mapping: which columns a field occupies, and how those columns are linked to other schema components.

7.3.1. Importing and Exporting Mapping Data

The mapping tool has the ability to import object-relational mapping data into the mapping factory, and to export mapping data from the mapping factory. We discuss the XML format used for imports and exports here.

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.12. 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.13. 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