Generate JOOQ Schema from file

2.4k views Asked by At

It is possible to generate Java classes using JOOQ from sql file without database connection ? I tried to specify inputSchema tag, but I got exception:

WARNING: SQL exception            : Exception while executing meta query: Cannot execute query. No Connection configured

My configuration looks like this:

<configuration>
                <generator>
                    <database>
                        <name>org.jooq.util.postgres.PostgresDatabase</name>
                        <inputSchema>filesystem:src/main/resources/schema.sql</inputSchema>
                        <includes>.*</includes>
                        <outputSchemaToDefault>true</outputSchemaToDefault>
                    </database>
                    <target>
                        <packageName>pckg.some</packageName>
                        <directory>target/generated-sources/jooq</directory>
                    </target>
                </generator>
</configuration>
1

There are 1 answers

0
Lukas Eder On BEST ANSWER

Yes, you can do such things, although not the way you expect. For one, jOOQ doesn't allow you to parse SQL statements and derive schema meta information this way. That would be way to complex to implement for all of the currently 20 supported RDBMS.

However, you have 2 options:

1. Run the SQL file prior to code generation

If your SQL file is sufficiently database vendor agnostic, you could run the file in an embedded database like H2, and then run the code generator from that database.

A similar approach is described in this blog post where JPA-annotated Java files are used to generate such a schema using Hibernate, and then run the jOOQ generator: http://vladmihalcea.com/jooq-facts-from-jpa-annotations-to-jooq-table-mappings

2. Use an intermediary XML file

jOOQ supports loading schema meta information from an XML file (or other meta sources). From the manual:

<!-- This value can be used to reverse-engineer standard jOOQ-meta XML formats

         org.jooq.util.xml.XMLDatabase

     You can also provide your own org.jooq.util.Database implementation
     here, if your database is currently not supported -->
<name>org.jooq.util.oracle.OracleDatabase</name>

The XSD schema for the XML file to be fed into XMLDatabase can be found here: http://www.jooq.org/xsd/jooq-meta-3.5.4.xsd

See also: http://www.jooq.org/doc/latest/manual/code-generation/codegen-configuration