Using JdbcPagingItemReader causing join statements causing MySQLIntegrityConstraintViolationException

寵の児 提交于 2019-12-05 17:49:22

Apparently this seems to be happening still in some form but there is way to craft your query so that the alias won't get removed. You rename the joined column by selecting it with different name.

Your selectClause becomes

SELECT owner.id, owner.first_name, owner.last_name, dog.dog_name

FromClause becomes

      FROM dog_owner owner
INNER JOIN (SELECT d2.id AS dogId, d2.dog_name
              FROM dog d2) AS dog
        ON owner.id = dog.dogId

Now you should be able to use "id" as sortKey as there is only one column with name "id". This works even if you add the dog.dogId into the selectClause.

You are correct in the reasoning why it is occurring. In your case, I'd expect using your alias to fix the issue. So you've aliased owner.id as ownerid, however you don't use it in the sortkey field (or the join clause for that matter). Use the alias and you should be ok.

We ran into this issue, Spring must get your sort key from the returned result set in order to page correctly. Under the hood sort key gets used in a WHERE clause which will not work with column aliases since WHERE gets processed before SELECT statements at the database level.

Our solution was to SELECT owner.id as "owner.id" so that the sort key can be retrieved from the result set using the same name. Doing SELECT owner.id puts the column id into your result set which in your case leads to issues with ambiguous columns. We preferred this approach to the previous answer's nested query.

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