2

I'm using jooq to generate pojo for my H2 db table

CREATE TABLE PUBLIC.ABC (
  ID          BIGINT AUTO_INCREMENT PRIMARY KEY,
  TRADE_DATE  DATE,
  STK_CODE    VARCHAR(63),
  REMARKS     TEXT,
  TIMESTAMP   TIMESTAMP NOT NULL
);

but the generated code (below)

{

    ...
    @Id
    @Column(name = "ID", unique = true, nullable = false, precision = 19)
    public Long getId() {
        return this.id;
    }
    ...

}

is missing the @GeneratedValue annotation which makes it impossible to insert new record with spring data rest repository since the passed in object always complain about id field not being set.

What config/work around can I do to get jooq working properly?

Below is the relevant pom file section i used to generate pojo at compile time:

<plugin>
    <groupId>org.jooq</groupId>
    <artifactId>jooq-codegen-maven</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2.version}</version>
        </dependency>
    </dependencies>
    <configuration>
        <!-- JDBC connection parameters -->
        <jdbc>
            <driver>org.h2.Driver</driver>
            <url>jdbc:h2:${user.home}/</url>
        </jdbc>
        <!-- Generator parameters -->
        <generator>
            <database>
                <name>org.jooq.util.h2.H2Database</name>
                <includes>.*</includes>
                <schemata>
                    <schema>
                        <inputSchema>PUBLIC</inputSchema>
                    </schema>
                </schemata>
            </database>
            <target>
                <packageName>org.abc</packageName>
                <directory>target/generated-sources/jooq</directory>
            </target>
            <generate>
                <pojos>true</pojos>
                <jpaAnnotations>true</jpaAnnotations>
            </generate>
        </generator>
    </configuration>
</plugin>

WORKAROUND

End up going the replacer route, plugin code as below for anyone running into the same issues before the feature's added:

    <plugin>
        <groupId>com.google.code.maven-replacer-plugin</groupId>
        <artifactId>replacer</artifactId>
        <version>1.5.3</version>
        <executions>
            <execution>
                <phase>prepare-package</phase>
                <goals>
                    <goal>replace</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <basedir>${project.basedir}/${jooq.gen.dir}</basedir>
            <filesToInclude>tables/pojos/*.java</filesToInclude>
            <replacements>
                <replacement>
                    <token>@Id</token>
                    <value>@Id @javax.persistence.GeneratedValue</value>
                </replacement>
            </replacements>
        </configuration>
    </plugin>
John
  • 660
  • 10
  • 26

1 Answers1

1

As of jOOQ 3.7, that's a missing feature. See: https://github.com/jOOQ/jOOQ/issues/5009

You have several workaround options:

  1. Patch the generated code using a search-replace Maven plugin by replacing @Id by @Id @javax.persistence.GeneratedValue(javax.persistence.GenerationType.IDENTITY) (assuming that all your primary keys are AUTO_INCREMENT)
  2. Patch jOOQ-codegen's JavaGenerator's org.jooq.util.JavaGenerator.printColumnJPAAnnotation() method and add that code yourself.
Community
  • 1
  • 1
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509