简体   繁体   中英

How use JOOQ to generate Stubs for PostGres using H2 - ENUM Type

During my CI build I generate Stubs using JOOQ maven plugin for PostGres. (I was not able to spinn a dedicated PostGres/Embedded Postgres in a Container next to my CI run easily). And H2 is more handy during Automated builds/test runs.

What I do is, run H2 with a PostGres Mode to generate the classes using the maven plugin

  <plugin>
        <groupId>org.jooq</groupId>
        <artifactId>jooq-codegen-maven</artifactId>
        <version>3.11.3</version>

        <dependencies>
          <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2.version}</version>
          </dependency>

          <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.4.jre7</version>
          </dependency>

        </dependencies>

        <executions>
          <execution>
            <id>jooq-codegen</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <jdbc>
                <driver>org.h2.Driver</driver>
                <url>jdbc:h2:./jooq;AUTO_SERVER=TRUE;MODE=PostgreSQL</url>
                <user>sa</user>
                <password></password>
              </jdbc>
              <generator>
                <name>org.jooq.codegen.JavaGenerator</name>
                <database>
                  <name>org.jooq.meta.h2.H2Database</name>
                  <includes>.*</includes>
                  <!-- we don't want the flyway stuff in the code -->
                  <excludes>Flyway_.*</excludes>
                  <inputSchema>PUBLIC</inputSchema>
                </database>

                <!-- Put the Custom Generator Code here -->
                <generate>
                  <deprecated>false</deprecated>
                  <instanceFields>true</instanceFields>
                  <pojos>true</pojos>


                </generate>
                <target>
                  <packageName>com.something.jooq</packageName>
                  <directory>target/generated-sources/jooq-h2</directory>
                </target>


              </generator>
            </configuration>
          </execution>
        </executions>
      </plugin>

This works perfect, unless I start with ENUMs. Wheres JOOQ generates an EnumType for each Usage (!) of the ENUM when the basis is a H2 db, there is only one EnumType when using the PostGres (this is how I'd expect it to work).

Eg:

I create an Enum Type in the DB

create type state as enum ('start', 'stop')

and I use this type in two tables, JOOQ using the H2 in PostGre Modus generates two Enums.

So if I switch between PostGres and H2, there generated Code changes and the implementation as well.

Is there a possible to force JOOQ to generate the same classes although the databases are different?

The current (still quite experimental) implementation of H2's enum type corresponds more to that of MySQL (per-table type) than PostgreSQL (per-schema type), even if the CREATE TYPE statement support is emulated in H2. See this issue for details: https://github.com/h2database/h2database/issues/1261

So, as it stands now, you cannot emulate PostgreSQL's enum types on H2.

Side note on using H2

Having said so, as soon as you use any vendor specific functionality (and you should, because PostgreSQL is awesome and has tons of them), your approach of using H2 because it seems to be easier is much flawed. You will run into limitations like this particular one time and again, and you will spend more time working around these limitations than you would if you had properly set up a PostgreSQL instance for the task. I highly recommend not using H2 in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM