All versions: 3.11 | 3.10 | 3.9 | 3.8 | 3.7 | 3.6 | Development versions: 3.12

By default, jOOQ's code generator takes live database connections as a database meta data source. In many project setups, this might not be optimal, as the live database is not always available.

One way to circumvent this issue is by providing jOOQ with a database meta definition file in XML format and by passing this XML file to jOOQ's XMLDatabase.

The XMLDatabase can read a standardised XML file that implements the http://www.jooq.org/xsd/jooq-meta-3.11.0.xsd schema. Essentially, this schema is an XML representation of the SQL standard INFORMATION_SCHEMA, as implemented by databases like H2, HSQLDB, MySQL, PostgreSQL, or SQL Server.

An example schema definition containing simple schema, table, column definitions can be seen below:

<?xml version="1.0"?>
<information_schema xmlns="http://www.jooq.org/xsd/jooq-meta-3.11.0.xsd">
    <schemata>
        <schema>
            <schema_name>TEST</schema_name>
        </schema>
    </schemata>

    <tables>
        <table>
            <table_schema>TEST</table_schema>
            <table_name>AUTHOR</table_name>
        </table>
        <table>
            <table_schema>TEST</table_schema>
            <table_name>BOOK</table_name>
        </table>
    </tables>

    <columns>
        <column>
            <table_schema>PUBLIC</table_schema>
            <table_name>AUTHOR</table_name>
            <column_name>ID</column_name>
            <data_type>NUMBER</data_type>
            <numeric_precision>7</numeric_precision>
            <ordinal_position>1</ordinal_position>
            <is_nullable>false</is_nullable>
        </column>
        ...
    </columns>
</information_schema>

Constraints can be defined with the following elements:

    <table_constraints>
        <table_constraint>
            <constraint_schema>TEST</constraint_schema>
            <constraint_name>PK_AUTHOR</constraint_name>
            <constraint_type>PRIMARY KEY</constraint_type>
            <table_schema>TEST</table_schema>
            <table_name>AUTHOR</table_name>
        </table_constraint>
        ...
    </table_constraints>

    <key_column_usages>
        <key_column_usage>
            <constraint_schema>TEST</constraint_schema>
            <constraint_name>PK_AUTHOR</constraint_name>
            <table_schema>TEST</table_schema>
            <table_name>AUTHOR</table_name>
            <column_name>ID</column_name>
            <ordinal_position>1</ordinal_position>
        </key_column_usage>
        ...
    </key_column_usage>
    
    <referential_constraints>
        <referential_constraint>
            <constraint_schema>TEST</constraint_schema>
            <constraint_name>FK_BOOK_AUTHOR_ID</constraint_name>
            <unique_constraint_schema>TEST</unique_constraint_schema>
            <unique_constraint_name>PK_AUTHOR</unique_constraint_name>
        </referential_constraint>
        ...
    </referential_constraints>
</information_schema>

The above file can be made available to the code generator configuration by using the XMLDatabase as follows:

<generator>
    <database>
        <name>org.jooq.meta.xml.XMLDatabase</name>
        <properties>
            
            <!-- Use any of the SQLDialect values here -->
            <property>
                <key>dialect</key>
                <value>ORACLE</value>
            </property>
            
            <!-- Specify the location of your database file -->
            <property>
                <key>xml-file</key>
                <value>src/main/resources/database.xml</value>
            </property>
        </properties>
    </database>
</generator>

If you already have a different XML format for your database, you can either XSL transform your own format into the one above via an additional Maven plugin, or pass the location of an XSL file to the XMLDatabase by providing an additional property:

<generator>
    <database>
        <name>org.jooq.meta.xml.XMLDatabase</name>
        <properties>
            
            ...
            
            <!-- Specify the location of your xsl file -->
            <property>
                <key>xsl-file</key>
                <value>src/main/resources/transform-to-jooq-format.xsl</value>
            </property>
        </properties>
    </database>
</generator>

This XML configuration can now be checked in and versioned, and modified independently from your live database schema.

The jOOQ Logo