Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INSERT..RETURNING is not working in JOOQ

I have a MariaDB database and I'm trying to insert a row in my table users. It has a generated id and I want to get it after insert. I have seen this but it's not working for me:

public Integer addNewUser(String name) {
    Record record = context.insertInto(table("users"), field("name"))
        .values(name)
        .returning(field("id"))
        .fetchOne();
    return record.into(Integer.class);
}

New row is inserted but record is always null. I'm not using JOOQ code generation.

like image 672
Héctor Avatar asked Dec 21 '16 13:12

Héctor


1 Answers

This is a known limitation in jOOQ 3.9: https://github.com/jOOQ/jOOQ/issues/2943

You currently cannot use the RETURNING clause in jOOQ when using plain SQL, because jOOQ needs to know the identity column name to bind to JDBC (in most databases). Unfortunately, passing the ID column to the RETURNING clause isn't sufficient, because there's no guarantee that this is the identity column. You might also pass several columns to the RETURNING clause, in case of which jOOQ wouldn't know which one would be the identity column.

like image 167
Lukas Eder Avatar answered Nov 03 '22 07:11

Lukas Eder