jOOQ insert query with returning generated keys

南楼画角 提交于 2019-12-08 16:19:50

问题


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

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?

thanks charis


回答1:


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

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)

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/

Or the Javadoc for creating INSERT statements that return values:

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




回答2:


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) { }



回答3:


Try

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


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


来源:https://stackoverflow.com/questions/8127378/jooq-insert-query-with-returning-generated-keys

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!