问题
I am running Spring Batch and using JdbcPagingItemReader. With a sample config of :
<bean id="dogQueryProvider" class="org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean">
<property name="databaseType" value="mysql" />
<property name="dataSource" ref="dataSource" />
<property name="selectClause"
value="SELECT owner.id as ownerid, first_name, last_name, dog_name " />
<property name="fromClause"
value="FROM dog_owner owner INNER JOIN dog ON owner.id = dog.id " />
<property name="sortKey" value="owner.id" />
</bean>
I am getting an error related to:
Column 'id' in order clause is ambiguous; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'id' in order clause is ambiguous.
Take it that dog_owner and dog tables have id columns. I am thinking that this is related to AbstractSqlPagingQueryProvider.getSortKeysWithoutAliases, which I think strips off owner from owner.id specified as sortKey. Any suggestions to resolve this issue?
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/28989996/using-jdbcpagingitemreader-causing-join-statements-causing-mysqlintegrityconstra