简体   繁体   中英

Java jOOQ issue when trying to update a table using a POJO Record

I'm trying to execute a simple updateRecord function, but it is giving me an error that I can't find the cause or any other people having it.

An example test project can be found at: https://github.com/billbarni/jooq-studer-h2-test

Java code:

import static mypackage.database.model.h2.public_.Tables.EXPRESSAO;
import mypackage.database.model.h2.public_.tables.pojos.Expressao;
import mypackage.database.model.h2.public_.tables.records.ExpressaoRecord;

public void updateQuery(Expressao expressaoPojo) {
  ExpressaoRecord expressaoRecord = ctx.newRecord(EXPRESSAO, expressaoPojo);
  ctx.executeUpdate(expressaoRecord); // Error with this parameter
}

Database create expression:

CREATE TABLE
 expressao (
  id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
  nome VARCHAR(50) NOT NULL,
  conteudo VARCHAR NOT NULL,
  qtd_tempo INT NOT NULL,
  tipo_tempo VARCHAR(5) NOT NULL,
  data_inicial TIMESTAMP NOT NULL
 );

Gradle generator configs:

plugins {
    id 'nu.studer.jooq' version '2.0.9'
}

dependencies {
    compile group: 'org.jooq', name: 'jooq-codegen', version: '3.10.4'
    jooqRuntime 'com.h2database:h2:1.4.197'
}

jooq {
    h2(sourceSets.main) {
        jdbc {
            driver = 'org.h2.Driver'
            url = 'jdbc:h2:file:./db'
            user = 'sa'
            password = ''
        }
        generator {
            name = 'org.jooq.util.DefaultGenerator'
            strategy {
                name = 'org.jooq.util.DefaultGeneratorStrategy'
            }
            database {
                name = 'org.jooq.util.h2.H2Database'
            }
            generate {
                relations = true
                deprecated = false
                records = true
                immutablePojos = true
                fluentSetters = true
            }
            target {
                packageName = 'mypackage.database.model.h2'
                directory = 'src/main/java'
            }
        }
    }
}

Java gives me this error before compiling:

executeUpdate (R) in DSLContext cannot be applied to (mypackage.database.model.h2.public_.tables.records.ExpressaoRecord) reason: no instance(s) of type variable(s) R exist so that ExpressaoRecord conforms to UpdatableRecord

What is the reason behind this issue? What am I doing wrong?

Obs.: I have 2 databases (firebird and h2) and I use jOOQ generator from Gradle to generate pojos and other classes automatically. They are not sharing POJOs or anything complex. The project is VERY small and simple.

Obs 2.: I used several versions of jOOQ libraries (from 3.9 to the new 3.11) and the problem persists.

Lukas Eder, god of jOOQ, I await for your return to save me from this verbose slumber.

DSLContext.executeUpdate(R) requires R to be a subtype of UpdatableRecord , which makes sense. Only a record that is "updatable" (ie knows its primary key) can effectively be updated.

It appears that your ExpressaoRecord is not an UpdatableRecord , but just a TableRecord . This can have several reasons:

You deactivated the <relations/> flag in the code generator

<relations>false</relations> can be used to turn off the generation of all relationship related features, including primary key, foreign key information. Without this information, you effectively cannot generate UpdatableRecord types.

Your table doesn't have a primary key.

Without a primary key, the code generator doesn't generate an UpdatableRecord .

You can either add a primary key to your table, or tell the code generator about a "synthetic primary key":

https://www.jooq.org/doc/latest/manual/code-generation/codegen-advanced/codegen-config-database/codegen-database-synthetic-primary-keys/

Or, you can "override" your primary key by treating any unique key as the primary key:

https://www.jooq.org/doc/latest/manual/code-generation/codegen-advanced/codegen-config-database/codegen-database-override-primary-keys/

The problem seems to be with the nu.studer.jooq plugin, since when using another code generator the H2 database model classes that reflect tables with primary keys recieves the UpdatableTable extension/implementation.

I opened an issue on the plugin, for those who want to follow it, you can check the open issues in the plugin github page already linked a few lines above.

The new releases of the plugin (versions 3.x+) seem to have fixed the issue. Be careful when upgrading, since there are many breaking changes in the plugin config and it forces the usage of the new jOOQ version (3.11+) also.

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