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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With