简体   繁体   English

jOOQ插入查询返回生成的键

[英]jOOQ insert query with returning generated keys

I installed jOOQ into eclipse, generated classes for my mySQL, but I still have problems to write also some basic queries. 我在jclipse中安装了jOOQ,为mySQL生成了类,但是我仍然有问题要写一些基本的查询。

I tried to compose insert query with returning of generated keys, but compiler throws error 我试图通过返回生成的密钥来组合插入查询,但编译器抛出错误

Table: tblCategory Columns: category_id, parent_id, name, rem, uipos 表:tblCategory列: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? 还尝试了其他不同的方法如何正确地做到这一点?

thanks charis 谢谢charis

The syntax you're using is for inserting multiple records. 您正在使用的语法是插入多个记录。 This is going to insert 4 records, each with one field. 这将插入4条记录,每条记录包含一个字段。

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

But you declared 4 fields, so that's not going to work: 但你宣布了4个字段,所以这不会起作用:

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

What you probably want to do is this: 你可能想做的是:

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

Or alternatively: 或者:

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

Probably, you're even better off by using 也许,通过使用你会更好

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

For more details, consider the manual: 有关更多详细信息,请参阅手册:

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

Or the Javadoc for creating INSERT statements that return values: 或者用于创建返回值的INSERT语句的Javadoc:

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

preffered SOLUTION 优先解决方案

  try {
    TblcategoryRecord record = (TblcategoryRecord) 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)
      .fetchOne();

      node.setId(record.getCategoryId());

    } catch (SQLException e1) { }

Try 尝试

YoutableRecord result = create
.insertInto(YOURTABLE)
.set(YOURTABLE.PROD_NAME, "VAL")
.returning(YOURTABLE.ID_PR)
.fetchOne();


int id = result.getValue(Products.PRODUCTS.ID_PR);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM