我有一个PostgreSQL uris 带串行表(自动递增) uri_id 列和字符串 uri 列。我可以使用jOOQ查询表:

createDSLContext().select(fieldByName("uri_id")).from(tableByName("uris"))
    .where(fieldByName("uri").equal(uri.toString())).fetchOne(0, Integer.class))

返回一个Java Integer.但是当我插入一个新的URI时,我想取回生成的 uri_id 键,所以我试试这个:

createDSLContext().insertInto(tableByName("uris"), fieldByName("uri"))
    .values(uri.toString()).returning(fieldByName("uri_id")).fetchOne().getValue(0, Integer.class)

这次我得到一个错误:

Exception in thread "main" java.lang.IllegalArgumentException: Field 0 is not contained in list

作为一个测试,我试图提供一个字面值 uri_idINSERT 声明,但仍然得到了错误。

看起来正在生成正确的SQL:

insert into "uris" ("uri") values ('http://example.com/') returning "uri_id"

但返回的记录是空的。即使我指定了一个字面量,这也是如此 uri_id 在insert语句中。

如何从PostgreSQL中检索自动生成的列 INSERT 使用jOOQ的语句?

有帮助吗?

解决方案

这个问题也在更详细地讨论 jOOQ用户组.

从本质上讲,你遇到了一个已知的问题, RETURNING 当与普通SQL一起使用时,jooq3.3(及更低版本)目前不支持子句,而不是与生成的表一起使用。这是问题参考:

https://github.com/jOOQ/jOOQ/issues/2374

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top