java – 在JOOQ中插入MySQL的返回查询

我试图使用以下代码来获取自动生成的ID.我的后端是MySQL.代码看起来像这样Record record = create.insertInto(CANDIDATE, CANDIDATE.FIRST_NAME, CANDIDATE.LAST_NAME,CANDIDATE.EXTRACTED_NAME).values(Charl...

我试图使用以下代码来获取自动生成的ID.我的后端是MySQL.代码看起来像这样

Record record = create.insertInto(CANDIDATE, CANDIDATE.FIRST_NAME,      
       CANDIDATE.LAST_NAME,CANDIDATE.EXTRACTED_NAME)
       .values("Charlotte", "Roche","Charlotte Roche")
       .returning(CANDIDATE.ID)
       .fetchOne();

System.out.println(record.getValue(CANDIDATE.ID));

我收到了NullPointerException.我看了一眼http://www.jooq.org/javadoc/latest/org/jooq/InsertReturningStep.html.
它说

Derby, H2, Ingres, MySQL, SQL Server only allow for retrieving IDENTITY column values as “generated key”. If other fields are requested, a second statement is issued. Client code must assure transactional integrity between the two statements.

根据我在Mysql中的理解,auto_increment可以作为IDENTITY使用.请问有谁可以说明如何为MySQL实现这一目标

我看过这个SO Question on a similar topic并尝试了以下

Result<?>  record =
            create.insertInto(CANDIDATE, CANDIDATE.FIRST_NAME, CANDIDATE.LAST_NAME,CANDIDATE.EXTRACTED_NAME)
                  .values("Charlotte", "Roche","Charlotte Roche")
                  .returning(CANDIDATE.ID)
                  .fetch();

            System.out.println(record.size());

虽然它在后端插入记录但它将record.size()打印为零

解决方法:

我知道我迟到了.
但我希望我能帮助有类似问题的人,

Derby, H2, Ingres, MySQL, SQL Server only allow for retrieving IDENTITY column values as “generated key“. If other fields are requested, a second statement is issued. Client code must assure transactional integrity between the two statements.

“生成密钥”这个词就是问题所在.
您可以使用SHOW CREATE TABLE $table_name检查您的表ID是否为AUTO_INCREMENT.我认为不是.

P / s:我正在使用MySQL

本文标题为:java – 在JOOQ中插入MySQL的返回查询

基础教程推荐