Spring Batch Paging with sortKeys and parameter values

寵の児 提交于 2019-12-04 16:41:24

I solved this problem with some intense debugging. It turns out that MySqlPagingQueryProvider utilizes a method getSortKeysWithoutAliases() when it builds up the SQL query to run for the first page and for subsequent pages. It therefore appends and (p.id > :_id) instead of and (p.id > :_p.id). Later on, when the second page sort values are created and stored in JdbcPagingItemReader's startAfterValues field it will use the original "p.id" String specified and eventually put into the named parameter map the pair ("_p.id",10). However, when the reader tries to fill in _id in the query, it doesn't exist because the reader used the non-alias removed key.

Long story short, I had to remove the alias reference when defining my sort keys.

provider.setSortKeys("p.id": Order.ASCENDING)

had to change to in order for everything to work nicely together

provider.setSortKeys("id": Order.ASCENDING)

I had the same issue and got another possible solution.

My table T has a primary key field INTERNAL_ID.

The query in JdbcPagingItemReader was like this:

SELECT INTERNAL_ID, ... FROM T WHERE ... ORDER BY INTERNAL_ID ASC

So, the key is: in some conditions, the query didn't return results, and then, raised the error above No value supplied for...

The solution is:

  • Check in a Spring Batch decider element if there are rows.
  • If it is, continue with chunk: reader-processor-writer.
  • It it's not, go to another step.

Please, note that they are two different scenarios:

  • At the beginning, there are rows. You get them by paging and finally, there are no more rows. This has no problem and decider trick is not required.
  • At the beginning, there are no rows. Then, this error raised, and the decider solved it.

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!