INSERT..RETURNING 在 JOOQ 中不起作用

2022-01-15 00:00:00 mariadb mysql java jooq

我有一个 MariaDB 数据库,我正在尝试在我的表 users 中插入一行.它有一个生成的 id ,我想在插入后得到它.我见过 这个 但它对我不起作用:

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);
}

插入了新行,但 record 始终为 null.我没有使用 JOOQ 代码生成.

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

推荐答案

这是 jOOQ 3.9 中的一个已知限制:https://github.com/jOOQ/jOOQ/issues/2943

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

在使用纯 SQL 时,您目前不能在 jOOQ 中使用 RETURNING 子句,因为 jOOQ 需要知道标识列名称才能绑定到 JDBC(在大多数数据库中).不幸的是,将 ID 列传递给 RETURNING 子句是不够的,因为不能保证这是标识列.您还可以将几列传递给 RETURNING 子句,以防 jOOQ 不知道哪一列是标识列.

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.

相关文章