Table of Contents
Kodo JDO 2.3 includes a preview release of our upcoming reverse mapping tool. The reverse mapping tool generates persistent class definitions, complete with JDO metadata and Kodo mapping extensions, from an existing database schema. The tool is currently in its beta stages of development; bug reports are very much appreciated. The reverse mapping tool has only been tested with Hypersonic SQL 7.1, SQLServer (MS JDBC driver), and Oracle; it is unlikely to work well with other databases at this point.
![]() |
Note |
|---|---|
|
The reverse mapping tool uses a new logging mechanism from our R&D department that is not yet avaialble in the main Kodo JDO release. The mechanism is based on the commons logging framework from Apache, which can integrate with Log4J, Java 1.4 logging, and others. See the commons logging API documentation for details on configuring commons logging. This means that the com.solarmetric.kodo.Logger configuration property is ignored by the reverse mapping tool. | |
To use the reverse mapping tool, follow the steps below:
Set the com.solarmetric.kodo.impl.jdbc.Schemas configuration property to a comma-separated list of the names of the target database schemas. If you do not use use explicit database schemas, you can skip this step (in this case Kodo JDO will attempt to filter out system tables on its own; for example under Oracle it filters all tables with a schema of MDSYS, SYS, or SYSTEM).
Note that some databases require schema names to be in a certain case. If you are using Oracle, for example, your schema names must be in all upper case, regardless of what case you used when creating the schema.
Use the R&D schema generator to export your current schema to an XML schema file. The R&D schema generator can be run via the included rd-schemagen script or through its Java class, com.solarmetric.rd.kodo.impl.jdbc.schema.SchemaGenerator. For example:
rd-schemagen -properties myprops.properties -file schema.xml
Examine the generated schema file. JDBC drivers often provide incomplete or faulty metadata, in which case the file will not exactly match the actual schema. Alter the XML file to match the true schema. The DTD for the schema file is given here.
After fixing any errors in the schema file, modify the XML to include foreign keys between all related tables. The schema generator will have automatically detected existing foreign key constraints; many schemas, however, do not employ database foreign keys for every table relation. By manually adding any missing foreign keys, you will give the reverse mapping tool the information it needs to create the proper relations between the persistent classes it creates.
Run the reverse mapping tool on the finished schema file, supplying the package name for the generated classes. (If you do not supply the schema file to reverse map, the tool will run directly against the schema in the database; if you do not supply a package, the tool will generate classes without package declarations.) The tool can be run via the included rd-reversemappingtool script, or through its Java class, com.solarmetric.rd.kodo.impl.jdbc.meta.ReverseMappingTool. For example:
rd-reversemappingtool -properties myprops.properties -package com.xyz.jdo schema.xml
Running the tool will generate .java files for each generated class, a single <package-name>.jdo metadata file for the package, a <package-name>.schema file, and a <package-name>.mapping file. The schema file is there to show you what portion of the schema was mapped; it serves no other purpose and can be deleted if desired. The mapping file contains the O/R mapping information for the generated classes. Mapping files like these will be offered as an optional alternative to O/R metadata extensions in a future version of Kodo JDO. For now, you must import the mapping information into Kodo JDO metadata extensions.
To import the generated O/R mapping information into metadata extensions, run the R&D import tool on the mapping file. The tool can be invoked via the included rd-importtool script or through its Java class, com.solarmetric.rd.kodo.impl.jdbc.meta.compat.ImportTool. Make sure to compile the generated classes before the import:
javac *.java rd-importtool -properties myprops.properties jdo.mapping
You can now delete the mapping file if desired.
Examine the generated classes and metadata, and modify them as necessary. Remember to register them with the schematool or list them in your persistent types property before use, as described here.
<!ELEMENT schemas (schema)+> <!ELEMENT schema (table)*> <!ATTLIST schema name CDATA #IMPLIED> <!ELEMENT table (column|index|pk|fk)+> <!ATTLIST table name CDATA #REQUIRED> <!ELEMENT column EMPTY> <!ATTLIST column name CDATA #REQUIRED> <!ATTLIST column type (array|bigint|binary|bit|blob|char|clob|date|decimal|distinct|double|float|integer|java_object|longvarbinary|longvarchar|null|numeric|other|real|ref|smallint|struct|time|timestamp|tinyint|varbinary|varchar) #REQUIRED> <!ATTLIST column not-null (true|false) "false"> <!ATTLIST column default CDATA #IMPLIED> <!ATTLIST column size CDATA #IMPLIED> <!ATTLIST column decimal-digits CDATA #IMPLIED> <!-- the 'column' attribute of indexes, pks, and fks can be used --> <!-- when the element has only one column (or for foriegn keys, --> <!-- only one local column); in these cases the on/join child --> <!-- elements can be left out --> <!ELEMENT index (on)*> <!ATTLIST index name CDATA #REQUIRED> <!ATTLIST index column CDATA #IMPLIED> <!ATTLIST index unique (true|false) "false"> <!ELEMENT pk (on)*> <!ATTLIST pk name CDATA #IMPLIED> <!ATTLIST pk column CDATA #IMPLIED> <!ELEMENT on EMPTY> <!ATTLIST on column CDATA #REQUIRED> <!ELEMENT fk (join)*> <!ATTLIST fk name CDATA #IMPLIED> <!ATTLIST fk to-table CDATA #REQUIRED> <!ATTLIST fk column CDATA #IMPLIED> <!ATTLIST fk delete-action (cascade|default|exception|none|null) "none"> <!ELEMENT join EMPTY> <!ATTLIST join column CDATA #REQUIRED> <!ATTLIST join to-column CDATA #REQUIRED>