Search code examples
javamavenjooq

Jooq generating different filenames for different databases


So i'm currently using jooq in a learning environment and was hoping for something that was simple to drop in other databases. Originally had an sqlite database and migrated to mariadb using the following pom configurations for maven only difference being the db url and Driver and meta

            <plugin>
                <groupId>org.jooq</groupId>
                <artifactId>jooq-codegen-maven</artifactId>
                <version>3.13.1</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <jdbc>
                        <driver>org.mariadb.jdbc.Driver</driver>
                        <!--suppress UnresolvedMavenProperty -->
                        <url>${db.url}</url>
                    </jdbc>
                    <generator>
                        <database>
                            <name>org.jooq.meta.mariadb.MariaDBDatabase</name>
                            <includes>.*</includes>
                        </database>
                        <target>
                            <packageName>org.learn_java.db.autogen</packageName>
                        </target>
                    </generator>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jooq</groupId>
                <artifactId>jooq-codegen-maven</artifactId>
                <version>3.13.1</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <jdbc>
                        <driver>com.mysql.cj.jdbc.Driver</driver>
                        <!--suppress UnresolvedMavenProperty -->
                        <url>${db.url}</url>
                    </jdbc>
                    <generator>
                        <database>
                            <name>org.jooq.meta.sqlite</name>
                            <includes>.*</includes>
                        </database>
                        <target>
                            <packageName>org.learn_java.db.autogen</packageName>
                        </target>
                    </generator>
                </configuration>
            </plugin>

using a very simple schema with flyway

CREATE TABLE IF NOT EXISTS INFO
(
   TAG_NAME varchar(30) PRIMARY KEY,
   MESSAGE varchar(2000) NOT NULL
);

it generates the Info table in different places

.tables.INFO // for sqlite
.tables.Info.INFO // for mariadb

Is there a way to standardize where it generates the Table classes?


Solution

  • In SQLite, there are no schemas, i.e. only a default schema. In other database products, there is a notion of schema (and even catalog, in the case of SQL Server), which acts as a namespace for your database objects both in the database and in jOOQ generated code.

    You did not specify what schema to include for code generation, so jOOQ generates code for all available schemas (only one in SQLite, several in MariaDB), producing an additional sub package per schema. To prevent this, use <inputSchema>:

    <database>
        <name>org.jooq.meta.mariadb.MariaDBDatabase</name>
        <!-- This is case sensitive. Use the actual case from your database -->
        <inputSchema>Info</inputSchema>
        <includes>.*</includes>
    </database>
    

    See also the section of the manual about schema mapping. Alternatively, you could use a generator strategy to override the default package name of generated objects.