Shyama Praveena
Shyama Praveena

Reputation: 11

Disambiguate table with name `object` in Jooq CodeGen

I am moving from Jooq 3.14.16 to Jooq 3.19.10 I am using codegen tool to generate Java source for jooq entities.

In the Postgres DB schema there are several tables and one of them is named object.

After moving to Jooq 3.19.10, generated entities fail to compile with error "method does not override or implement a method from a supertype".

15:08:09,464 [ERROR] /Users/shyama/projects/jooqtest/src/main/java/com/jooqtest/db/test/tables/Account.java:[331,5] method does not override or implement a method from a supertype

Steps to reproduce: Create atleast 2 tables, one of them with name object, as;

CREATE TABLE jooqtest.account (
  id INT,
  details VARCHAR(32)
);

CREATE TABLE jooqtest.object (
  obj_id INT,
  details VARCHAR(32)
);

jooqtest.xml for cogegen input,

<configuration>
  <jdbc>
    <driver>org.postgresql.Driver</driver>
    <url>jdbc:postgresql://localhost:5432/supe</url>
    <user>supe</user>
    <password>xxxx</password>
  </jdbc>
  <generator>
    <database>
      <name>org.jooq.meta.postgres.PostgresDatabase</name>
      <includes>.*</includes>
      <excludes></excludes>
      <inputSchema>jooqtest</inputSchema>
    </database>
    <target>
      <packageName>org.jooqtest</packageName>
      <directory>entities</directory>
    </target>
  </generator>
</configuration>

Using jdk21 and running codegen in my Mac like below,

java -cp jOOQ-3.19.10/jOOQ-lib/r2dbc-spi-1.0.0.RELEASE.jar:jOOQ-3.19.10/jOOQ-lib/reactive-streams-1.0.3.jar:jOOQ-3.19.10/jOOQ-lib/jakarta.xml.bind-api-3.0.0.jar:jOOQ-3.19.10/jOOQ-lib/jooq-3.19.10.jar:jOOQ-3.19.10/jOOQ-lib/jooq-codegen-3.19.10.jar:jOOQ-3.19.10/jOOQ-lib/jooq-meta-3.19.10.jar:postgresql-42.7.3.jar:. org.jooq.codegen.GenerationTool jooqtest.xml

After jooq entity files are created, In tables/Account.java method override looks like below,

    /**
     * Create an inline derived table from this table
     */
    @Override
    @PlainSQL
    public Account where(@Stringly.SQL String condition, Object... binds) {
        return where(DSL.condition(condition, binds));
    }

Whereas, in tables/Object.java it looks like,

    /**
     * Create an inline derived table from this table
     */
    @Override
    @PlainSQL
    public Object where(@Stringly.SQL String condition, java.lang.Object... binds) {
        return where(DSL.condition(condition, binds));
    }

Here the Object in Account class is not able to resolve to java.lang.Object Hence compiler throws error.

Kindly suggest if there is any way to resolve it without renaming tables in DB?

I have provided Steps to Reproduce above. I am expecting to resolve compiler error via some settings in codegen, without renaming tables in my existing DB.

Adding details of the runtime: Following is the maven project's, root pom.xml content:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>testparentproject</artifactId>
    <version>1.0.0.develop-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>Parent Project for test</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- JDK -->
        <jdk.source.version>21</jdk.source.version>
        <jdk.target.version>21</jdk.target.version>
        <!-- Libraries -->
        <jooq.version>3.19.10</jooq.version>
        <!-- plugin versions -->
        <maven.compiler.plugin.version>3.13.0</maven.compiler.plugin.version>
        <maven.resources.plugin.version>3.3.1</maven.resources.plugin.version>
        <maven.jar.plugin.version>3.4.2</maven.jar.plugin.version>
    </properties>
    <modules>
        <module>testjooq</module>
    </modules>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven.compiler.plugin.version}</version>
                    <configuration>
                        <source>${jdk.source.version}</source>
                        <target>${jdk.target.version}</target>
                        <showDeprecation>true</showDeprecation>
                        <compilerArgument>-Xlint:all</compilerArgument>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>${maven.resources.plugin.version}</version>
                    <configuration>
                        <propertiesEncoding>UTF-8</propertiesEncoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>${maven.jar.plugin.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jooq</groupId>
                <artifactId>jooq</artifactId>
                <version>${jooq.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

Content of project testjooq/pom.xml is:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.test</groupId>
        <artifactId>testparentproject</artifactId>
        <version>1.0.0.develop-SNAPSHOT</version>
    </parent>
    <artifactId>testjooq</artifactId>
    <packaging>jar</packaging>
    <name>Jooq generated DB entities test</name>
    <dependencies>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq</artifactId>
        </dependency>
    </dependencies>
</project>

Folder structure after placing jooq generated code looks like below, enter image description here

Maven build command is mvn clean package.

Refer generated code in this repo: https://github.com/spsheni/jooqtest

Upvotes: 1

Views: 102

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 220867

Here are some workarounds:

Specify which types to always fully qualify

There's a configuration flag that allows for specifying which types should always be fully qualified in jOOQ's code generation output:

<configuration>
  <generator>
    <generate>
      <fullyQualifiedTypes>java\.lang\.Object</fullyQualifiedTypes>
    </generate>
  </generator>
</configuration>

That would be the simplest solution here.

Using a generator strategy to rename the org.jooqtest.Object

Programmatic or configurative generator strategies can help you rename the generated Object class to Object_ or whatever, e.g. when using the configurative approach:

<configuration>
  <generator>
    <strategy>
      <matchers>
        <tables>
          <table>
            <expression>OBJECT</expression>
            <pojoClass>
              <expression>$0_</expression>
              <transform>PASCAL</transform>
            </pojoClass>
          </table>
        </tables>
      </matchers>
    </strategy>
  </generator>
</configuration>

Turn off that particular code generation feature

You can turn off the generation of these where() method overrides:

<configuration>
  <generator>
    <generate>
      <whereMethodOverrides>false</whereMethodOverrides>
    </generate>
  </generator>
</configuration>

Upvotes: 0

Related Questions