jOOQ插入查询并返回生成的密钥 [英] jOOQ insert query with returning generated keys

查看:160
本文介绍了jOOQ插入查询并返回生成的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将jOOQ安装到eclipse中,为mySQL生成了类,但是我仍然无法编写一些基本查询.

I installed jOOQ into eclipse, generated classes for my mySQL, but I still have problems to write also some basic queries.

我试图通过返回生成的键来组成插入查询,但是编译器抛出错误

I tried to compose insert query with returning of generated keys, but compiler throws error

表:tblCategory 列:category_id,parent_id,名称,rem,uipos

Table: tblCategory Columns: category_id, parent_id, name, rem, uipos

Result<TblcategoryRecord> result= create.insertInto(Tblcategory.TBLCATEGORY, 
    Tblcategory.PARENT_ID, Tblcategory.NAME, Tblcategory.REM, Tblcategory.UIPOS)
        .values(node.getParentid())
        .values(node.getName())
        .values(node.getRem())
        .values(node.getUipos())
        .returning(Tblcategory.CATEGORY_ID)
        .fetch();

也尝试了其他不同的方式 怎么做才对呢?

tried also other differnt ways how to do it right way?

谢谢 查理斯

推荐答案

您使用的语法用于插入多个记录.这将插入4条记录,每条记录只有一个字段.

The syntax you're using is for inserting multiple records. This is going to insert 4 records, each with one field.

.values(node.getParentid())
.values(node.getName())
.values(node.getRem())
.values(node.getUipos())

但是您声明了4个字段,所以这行不通:

But you declared 4 fields, so that's not going to work:

create.insertInto(Tblcategory.TBLCATEGORY, 
  Tblcategory.PARENT_ID, Tblcategory.NAME, Tblcategory.REM, Tblcategory.UIPOS)

您可能想做的是这样:

Result<TblcategoryRecord> result = create
  .insertInto(Tblcategory.TBLCATEGORY, 
    Tblcategory.PARENT_ID, Tblcategory.NAME, Tblcategory.REM, Tblcategory.UIPOS)
  .values(node.getParentid(), node.getName(), node.getRem(), node.getUipos())
  .returning(Tblcategory.CATEGORY_ID)
  .fetch();

或者:

Result<TblcategoryRecord> result = create
  .insertInto(Tblcategory.TBLCATEGORY) 
  .set(Tblcategory.PARENT_ID, node.getParentid())
  .set(Tblcategory.NAME, node.getName())
  .set(Tblcategory.REM, node.getRem())
  .set(Tblcategory.UIPOS, node.getUipos())
  .returning(Tblcategory.CATEGORY_ID)
  .fetch();

也许,通过使用

TblcategoryRecord result =
  // [...]
  .fetchOne();

有关更多详细信息,请考虑手册:

For more details, consider the manual:

http://www.jooq .org/doc/2.6/manual/sql-building/sql-statements/insert-statement/

或用于创建返回值的INSERT语句的Javadoc:

Or the Javadoc for creating INSERT statements that return values:

http://www.jooq.org/javadoc/latest/org/jooq/InsertReturningStep.html

这篇关于jOOQ插入查询并返回生成的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆