NoSuchMethod in JOOQ 3.14 InsertQuery with Postgresql

Tags: , , ,



I’m trying to use Jooq’s InsertQuery to insert a record into PostgresQL database, but I’m running into following error:

java.lang.NoSuchMethodError: 'org.jooq.UniqueKey org.jooq.impl.Internal.createUniqueKey(org.jooq.Table, org.jooq.Name, org.jooq.TableField[], boolean)'
at fi.eriran.generated.jooq.Keys.<clinit>(Keys.java:36) ~[classes/:na]
at fi.eriran.generated.jooq.tables.User.getPrimaryKey(User.java:124) ~[classes/:na]
at org.jooq.impl.UpdatableRecordImpl.getPrimaryKey(UpdatableRecordImpl.java:119) ~[jooq-3.13.4.jar:na]
at org.jooq.impl.AbstractRecord.set(AbstractRecord.java:349) ~[jooq-3.13.4.jar:na]
at org.jooq.impl.AbstractRecord.set(AbstractRecord.java:325) ~[jooq-3.13.4.jar:na]
at fi.eriran.generated.jooq.tables.records.UserRecord.setUsername(UserRecord.java:44) ~[classes/:na]
at fi.eriran.criminalapi.main.dao.user.query.UserQuery.createInsertRecord(UserQuery.java:37) ~[classes/:na]
at fi.eriran.criminalapi.main.dao.user.query.UserQuery.insert(UserQuery.java:22) ~[classes/:na]

The query class I’m using looks like this:

@Component
public class UserQuery {

@Autowired
private DslContext ctx;

public InsertQuery<UserRecord> insert(NewUser newUser) {
    InsertQuery<UserRecord> insertQuery = ctx.insertQuery(USER);
    insertQuery.addRecord(createInsertRecord(newUser));
    insertQuery.setReturning();
    return insertQuery;
}

private UserRecord createInsertRecord(NewUser newUser) {
    UserRecord userRecord = new UserRecord();
    userRecord.setUsername(newUser.getUserName());
    userRecord.setPassword(newUser.getPassword());
    return userRecord;
}
}

The user database table looks like this. It has a unique key on the username column and a primary key on the id column.

CREATE TABLE criminal."user" (
id serial NOT NULL,
username varchar(500) NOT NULL,
"password" varchar(500) NOT NULL,
created_at timestamptz(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT user_pk PRIMARY KEY (id),
CONSTRAINT user_un UNIQUE (username)
);
CREATE INDEX user_username_idx ON criminal."user" (username);

If I do this using ctx.insertInto… it works fine. I initially used Jooq 3.14.0 but upgrading to the latest 3.14.4 did not help here. Something must be wrong with the generated Keys class since everything works until Keys class is initialized. Is something wrong with either the Postgresql table structure or could something have gone wrong with the code generation?

Full repo here: https://github.com/EriRan/Criminal-API/tree/security-attempt

Answer

You’re saying you’re using jOOQ 3.14, yet on your stack trace, there is a reference to version 3.13.4:

at org.jooq.impl.UpdatableRecordImpl.getPrimaryKey(UpdatableRecordImpl.java:119) ~[jooq-3.13.4.jar:na]

You probably have set up your classpath incorrectly. Both the jOOQ version to generate code, and the jOOQ version to run your code must be the same.



Source: stackoverflow