给定:
CREATE TABLE foo (id BIGINT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id));
我想调用:INSERT INTO foo VALUES()
并取回生成的 key 。我尝试按照 jOOQ insert query with returning generated keys 中的建议进行操作但是当我调用时:
RoomsRecord record = db.insertInto(foo, Collections<Field<?>>emptyList()).returning(foo.ID).fetchOne();
JOOQ 返回 null 而不是生成的键。这是错误吗?
最佳答案
这似乎是 FieldMapsForInsert.isExecutable() 中的错误。它将上述查询标记为不可执行,但它在 MySQL 下是合法的。我提交了 https://github.com/jOOQ/jOOQ/issues/20
您可以将 NULL
作为 workaround 插入.此外,您必须配置 JOOQ 以生成表“关系”,否则 returning()
将看不到主键的存在并返回 null。
<configuration>
<generator>
<generate>
<relations>true</relations>
</generate>
</generator>
</configuration>
关于java - JOOQ:返回自动递增的 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11056246/