jOOQ 插入查询并返回生成的键

发布于 2024-12-16 11:02:10 字数 595 浏览 4 评论 0 原文

我将 jOOQ 安装到 eclipse 中,为 mySQL 生成了类,但在编写一些基本查询时仍然遇到问题。

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

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

还尝试了其他不同的方式 如何正确地做到这一点?

谢谢 查理斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不如归去 2024-12-23 11:02:10

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

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

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

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

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

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

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

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

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

大姐,你呐 2024-12-23 11:02:10

首选解决方案

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

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) { }
飘逸的'云 2024-12-23 11:02:10

尝试

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


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

Try

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


int id = result.getValue(Products.PRODUCTS.ID_PR);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文