【问题标题】:JdbcPagingItemReader not working with join queryJdbcPagingItemReader 不适用于连接查询
【发布时间】:2020-08-18 19:33:06
【问题描述】:

我有两个表表 1 和表 2。两者都有相同的名称 id 列。

因为它是一个左外连接查询,所以我必须使用别名。

在 sortKeys 中,如果我提到 id ,错误是

Caused by: org.postgresql.util.PSQLException: ERROR: column reference "id" is ambiguous

在 sortKeys 中,如果我提到 t1.id ,错误是

Caused by: org.postgresql.util.PSQLException: The column name t1.id was not found in this ResultSet.

如何处理此类查询?

我正在使用最新的 Spring Boot 版本 2.2.5.RELEASE

我在 stackoverflow 上看到了与此问题相关的其他帖子。但是,这些都在旧版本的 Spring Batch 中,因此我不应该遇到这个问题。

【问题讨论】:

  • 您可以发布您正在使用的完整查询吗?

标签: spring-boot spring-batch


【解决方案1】:

@youness.bout 和另一位程序员的回答为我工作。我在这里记录了它https://github.com/spring-projects/spring-batch/issues/1208#issuecomment-994234546。这似乎是 Spring Batch 项目中的一个已知问题。

在这里重写答案:

  1. 在 select 子句中使用与列名相同的列名别名 - t1.table1_column1 as 't1.table1_column1'
  2. 在 select 子句中用单引号 (') 将列名别名括起来 - t1.table1_column1 as 't1.table1_column1'
  3. 使用列名别名作为排序键 - setSortKey("t1.table1_column1")

这是我在kotlin中所做的

    @Bean
    @StepScope
    fun readQueryProvider() = SqlPagingQueryProviderFactoryBean().apply {
        setDataSource(dataSource)
        setSelectClause("select t1.table1_column1 as 't1.table1_column1', t1.table1_column2,"
                + " t1.table1_column3, t2.table2_column2, t3.table3_column3")
        setFromClause("table1 t1"
                + " inner join table2 t2 on t1.table1_column1 = t2.table2_column1"
                + " inner join table3 t3 on t2.table2_column2 = t3.table3_column2"
                + " inner join table4 t4 on t1.table1_column1 = t4.table4_column1")
        setWhereClause("where t3.table3_column2 = :placeholder1" 
                + " and t2.table2_column3 = :placeholder2 and t4.table4_column2 = :placeholder3")
        setSortKey("t1.table1_column1")
    }

【讨论】:

    【解决方案2】:

    解决此问题的一种方法是将 t1.id 添加为投影并将其命名为其他名称,例如:

    select t1.id as id_t1 ....
    

    并在您的 sortKeys 中添加“id_t1”。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-24
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多