首页 > 解决方案 > Spring 集成 JDBC 轮询器。使用复合主键更新

问题描述

我正在使用 JDBC 轮询器来处理一些数据库记录,当工作流完成时,我需要更新这些记录。如果适用于具有复合键的表,我找不到一种方法。

这是我的例子。表事件。带有主键(DATETIME、EVENT_LOCATION、EVENT_TYPE)。我无法更改架构。

行映射到具有属性名称的 POJO:日期时间、位置、类型。

<int-jdbc:inbound-channel-adapter
    query="select * from events where uploaded = 0”
    channel="fromdb" data-source="dataSource"
    max-rows="${app.maxrows}"
    row-mapper=“eventRowMapper”
    update="update events set uploaded=1 where DATETIME =:dateTime AND EVENT_LOCATION=:location AND EVENT_TYPE = :type”&gt;
<int:poller fixed-delay="${app.intervalmsecs}" />

但是当轮询器尝试更新这些记录时,我从服务器收到语法错误响应。

阅读文档后,轮询器似乎使用 '(:id)' 来更新 rows ,但它假设一个单列 PK。我找不到任何有关更新主键中包含多列的行的信息

有没有办法用多列主键更新行?或者我应该使用出站 jdbc 还是编写自己的更新解决方案?

标签: spring-integration

解决方案


显示完整的堆栈跟踪;您的事件对象和行映射器;我刚刚更改了其中一项测试

    JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(embeddedDatabase,
            "select * from item where id not in (select id from copy)");
    adapter.setUpdateSql("insert into copy values(:id,10)");

    JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(embeddedDatabase,
            "select * from item where id not in (select foo from copy)");
    adapter.setUpdateSql("insert into copy values(:foo,:status)");

它工作得很好。

只要该列作为选择查询结果的属性出现,它就会起作用。(结果是由行映射器创建的对象)。

dateTimelocation并且type必须是 上的属性Event

此外,根据您的update-query,看起来您应该update-per-row设置为 true ,因为它只更新一行。


推荐阅读