You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过jOOQ ExecuteListener获取插入操作的强类型记录值?

问题

我正在使用jOOQ的ExecuteListener SPI实现预提交钩子,用来调用外部API,目标是传入部分插入值(逻辑会按表定制),如果API调用失败就回滚事务。

尝试方案1:通过ExecuteContext解析SQL

通过ExecuteContext可以获取查询、SQL语句及参数,但必须解析SQL字符串才能得知表名,代码如下:

public class WriteListener implements ExecuteListener {

  @Override
  public void end(ExecuteContext ctx) {
    if (ctx.type() == ExecuteType.WRITE) {
      // extract table, rows and their values
      final org.jooq.Query query = ctx.query(); // tried to cast to InsertQuery too
      final org.jooq.Record record = ctx.record(); // can't access the values
      // etc..
    }
    ExecuteListener.super.end(ctx);
  }
}

尝试方案2:使用实验性QOM API

可以读取表、列和字段,但无法无需反射提取行值,代码如下:

public class WriteListener implements ExecuteListener {

  @Override
  public void end(ExecuteContext ctx) {
    if (ctx.query() instanceof QOM.Insert insert) {
      final Table table = insert.$into();
      final var cols = insert.$columns();
      final var rows = insert.$values();
      // can't extract row values!
    }
    ExecuteListener.super.end(ctx);
  }
}

理想实现目标

我希望能将记录转为代码生成的强类型记录对象,从而获取填充好的字段值,理想代码示例如下(目前无法运行):

public class WriteListener implements ExecuteListener {

  @Override
  public void end(ExecuteContext ctx) {
    if (ctx.type() == ExecuteType.WRITE) {
      // NOTE: THIS DOES NOT WORK, IT'S AN IDEA ONLY.
      if (ctx.record() instanceof MyTableRowRecord record) {
        final DateTime createdAt = record.CREATED_AT;
        // invoke an external API call with the table name, and the created at value.
      }
    }
    ExecuteListener.super.end(ctx);
  }
}

我当前使用jOOQ社区版,如果该功能仅在其他版本提供,我愿意升级。

内容的提问来源于stack exchange,提问作者Ennabah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 09:27:22