Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 返回生成键的jOOQ insert查询_Java_Mysql_Sql_Jooq - Fatal编程技术网

Java 返回生成键的jOOQ insert查询

Java 返回生成键的jOOQ insert查询,java,mysql,sql,jooq,Java,Mysql,Sql,Jooq,我在eclipse中安装了jOOQ,为我的mySQL生成了类,但在编写一些基本查询时仍然存在问题 我试图用返回生成的键组合插入查询,但编译器抛出错误 表:TBL类别 列:类别id、父项id、名称、rem、uipos Result<TblcategoryRecord> result= create.insertInto(Tblcategory.TBLCATEGORY, Tblcategory.PARENT_ID, Tblcategory.NAME, Tblcategory.R

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

我试图用返回生成的键组合插入查询,但编译器抛出错误

表:TBL类别 列:类别id、父项id、名称、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();
Result-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();
还尝试了其他不同的方法 如何用正确的方法来做

谢谢
charis

您使用的语法用于插入多条记录。这将插入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();
详情请参阅手册:

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

首选解决方案

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


谢谢,添加了首选解决方案,但Eclipse仍然需要casting@Charis997:通常不建议在堆栈溢出问题中给出答案。后续访客将很难理解发生了什么。相反,你可以“接受”这个答案,或者自己提供一个答案,如果这个答案不清楚的话enough@Charis997:关于演员阵容:你说得对。这在Jooq2.0中不再是必需的。但是jOOQ 1.6.9仍然需要它,让我们尊重代码吧!推荐的解决方案张贴在下面的答案中。期待着Jooq2.0thxy,你真的不应该忽视SQLException!如果这只是一个示例,那么您可以将整个try/catch块排除在外。
TblcategoryRecord result =
  // [...]
  .fetchOne();
  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) { }
YoutableRecord result = create
.insertInto(YOURTABLE)
.set(YOURTABLE.PROD_NAME, "VAL")
.returning(YOURTABLE.ID_PR)
.fetchOne();


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