4.  Mapping Defaults

The previous sections showed how to use the mapping tool to generate default mappings. But how does the mapping tool know what mappings to generate? The answer lies in the org.apache.openjpa.jdbc.meta.MappingDefaults interface. OpenJPA uses an instance of this interface to decide how to name tables and columns, where to put foreign keys, and generally how to create a schema that matches your object model.

Important

OpenJPA relies on foreign key constraint information at runtime to order SQL appropriately. Be sure to set your mapping defaults to reflect your existing database constraints, set the schema factory to reflect on the database for constraint information (see Section 12.2, “ Schema Factory ”), or use explicit foreign key mappings as described in Section 7.9.2, “ Foreign Keys ”.

The openjpa.jdbc.MappingDefaults configuration property controls the MappingDefaults interface implementation in use. This is a plugin property (see Section 4, “ Plugin Configuration ”), so you can substitute your own implementation or configure the existing ones. OpenJPA includes the following standard implementations:

The example below turns on foreign key generation during schema creation and associates the org.mag.data.InfoStruct field type with the custom org.mag.mapping.InfoStructHandler value handler.

Example 7.12.  Configuring Mapping Defaults

<property name="openjpa.jdbc.MappingDefaults"
    value="ForeignKeyDeleteAction=restrict,
    FieldStrategies='org.mag.data.InfoStruct=org.mag.mapping.InfoStructHandler'"/>