diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcPagingItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcPagingItemReader.java index e6700e603..25896587b 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcPagingItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcPagingItemReader.java @@ -222,11 +222,19 @@ public class JdbcPagingItemReader extends AbstractPagingItemReader impleme logger.debug("SQL used for jumping: [" + jumpToItemSql + "]"); } - startAfterValue = simpleJdbcTemplate.getJdbcOperations().queryForObject(jumpToItemSql, new RowMapper() { + RowMapper startMapper = new RowMapper() { public Object mapRow(ResultSet rs, int i) throws SQLException { return rs.getObject(1); } - }); + }; + if (this.queryProvider.isUsingNamedParameters()) { + startAfterValue = simpleJdbcTemplate.getNamedParameterJdbcOperations().queryForObject(jumpToItemSql, + getParameterMap(parameterValues, startAfterValue), startMapper); + } + else { + startAfterValue = simpleJdbcTemplate.getJdbcOperations().queryForObject(jumpToItemSql, + getParameterList(parameterValues, startAfterValue).toArray(), startMapper); + } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractPagingItemReaderParameterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractPagingItemReaderParameterTests.java index 768bf704f..d934daf91 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractPagingItemReaderParameterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractPagingItemReaderParameterTests.java @@ -1,29 +1,31 @@ package org.springframework.batch.item.database; -import org.junit.Before; -import org.junit.After; -import org.junit.Test; -import org.junit.Assert; -import org.springframework.batch.item.ItemStream; -import org.springframework.batch.item.ItemReader; -import org.springframework.batch.item.ExecutionContext; -import org.springframework.batch.item.sample.Foo; -import org.springframework.beans.factory.annotation.Autowired; import javax.sql.DataSource; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.ItemStream; +import org.springframework.batch.item.sample.Foo; +import org.springframework.beans.factory.annotation.Autowired; + /** - * @author trisberg + * @author Thomas Risberg + * @author Dave Syer */ public abstract class AbstractPagingItemReaderParameterTests { - protected ItemReader tested; + + protected AbstractPagingItemReader tested; protected ExecutionContext executionContext = new ExecutionContext(); + @Autowired protected DataSource dataSource; @Before public void setUp() throws Exception { tested = getItemReader(); - ((ItemStream)tested).open(executionContext); } @After @@ -34,6 +36,8 @@ public abstract class AbstractPagingItemReaderParameterTests { @Test public void testRead() throws Exception { + ((ItemStream)tested).open(executionContext); + Foo foo3 = tested.read(); Assert.assertEquals(3, foo3.getValue()); @@ -47,5 +51,18 @@ public abstract class AbstractPagingItemReaderParameterTests { Assert.assertNull(o); } - protected abstract ItemReader getItemReader() throws Exception; + @Test + public void testReadAfterJump() throws Exception { + + executionContext.putInt(tested.getClass().getSimpleName()+".read.count", 2); + ((ItemStream)tested).open(executionContext); + + Foo foo5 = tested.read(); + Assert.assertEquals(5, foo5.getValue()); + + Object o = tested.read(); + Assert.assertNull(o); + } + + protected abstract AbstractPagingItemReader getItemReader() throws Exception; } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/IbatisPagingItemReaderParameterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/IbatisPagingItemReaderParameterTests.java index 6fb89eb70..c7fdf3c79 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/IbatisPagingItemReaderParameterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/IbatisPagingItemReaderParameterTests.java @@ -1,22 +1,22 @@ package org.springframework.batch.item.database; +import java.util.Collections; + import org.junit.runner.RunWith; import org.springframework.batch.item.sample.Foo; -import org.springframework.batch.item.ItemReader; -import org.springframework.orm.ibatis.SqlMapClientFactoryBean; import org.springframework.core.io.ClassPathResource; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.orm.ibatis.SqlMapClientFactoryBean; import org.springframework.test.context.ContextConfiguration; -import com.ibatis.sqlmap.client.SqlMapClient; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import java.util.Collections; +import com.ibatis.sqlmap.client.SqlMapClient; @SuppressWarnings("unchecked") @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/org/springframework/batch/item/database/data-source-context.xml") public class IbatisPagingItemReaderParameterTests extends AbstractPagingItemReaderParameterTests { - protected ItemReader getItemReader() throws Exception { + protected AbstractPagingItemReader getItemReader() throws Exception { SqlMapClientFactoryBean factory = new SqlMapClientFactoryBean(); factory.setConfigLocation(new ClassPathResource("ibatis-config.xml", getClass())); factory.setDataSource(dataSource); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcPagingItemReaderClassicParameterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcPagingItemReaderClassicParameterTests.java index b116bb3f5..f3d1b228e 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcPagingItemReaderClassicParameterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcPagingItemReaderClassicParameterTests.java @@ -5,7 +5,6 @@ import java.sql.SQLException; import java.util.Collections; import org.junit.runner.RunWith; -import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.database.support.HsqlPagingQueryProvider; import org.springframework.batch.item.sample.Foo; import org.springframework.jdbc.core.simple.ParameterizedRowMapper; @@ -17,7 +16,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration(locations = "/org/springframework/batch/item/database/JdbcPagingItemReaderParameterTests-context.xml") public class JdbcPagingItemReaderClassicParameterTests extends AbstractPagingItemReaderParameterTests { - protected ItemReader getItemReader() throws Exception { + protected AbstractPagingItemReader getItemReader() throws Exception { JdbcPagingItemReader reader = new JdbcPagingItemReader(); reader.setDataSource(dataSource); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcPagingItemReaderNamedParameterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcPagingItemReaderNamedParameterTests.java index 36f06b207..23a13166b 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcPagingItemReaderNamedParameterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcPagingItemReaderNamedParameterTests.java @@ -5,7 +5,6 @@ import java.sql.SQLException; import java.util.Collections; import org.junit.runner.RunWith; -import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.database.support.HsqlPagingQueryProvider; import org.springframework.batch.item.sample.Foo; import org.springframework.jdbc.core.simple.ParameterizedRowMapper; @@ -16,7 +15,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration(locations = "/org/springframework/batch/item/database/JdbcPagingItemReaderParameterTests-context.xml") public class JdbcPagingItemReaderNamedParameterTests extends AbstractPagingItemReaderParameterTests { - protected ItemReader getItemReader() throws Exception { + protected AbstractPagingItemReader getItemReader() throws Exception { JdbcPagingItemReader reader = new JdbcPagingItemReader(); reader.setDataSource(dataSource); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaPagingItemReaderNativeQueryIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaPagingItemReaderNativeQueryIntegrationTests.java index 56ea890c3..292117208 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaPagingItemReaderNativeQueryIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaPagingItemReaderNativeQueryIntegrationTests.java @@ -5,7 +5,6 @@ import java.util.Collections; import javax.persistence.EntityManagerFactory; import org.junit.runner.RunWith; -import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.database.orm.JpaNativeQueryProvider; import org.springframework.batch.item.sample.Foo; import org.springframework.beans.factory.annotation.Autowired; @@ -23,7 +22,7 @@ public class JpaPagingItemReaderNativeQueryIntegrationTests extends AbstractPagi @Autowired private EntityManagerFactory entityManagerFactory; - protected ItemReader getItemReader() throws Exception { + protected AbstractPagingItemReader getItemReader() throws Exception { String sqlQuery = "select * from T_FOOS where value >= :limit"; diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaPagingItemReaderParameterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaPagingItemReaderParameterTests.java index 7c70bd737..32814b8be 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaPagingItemReaderParameterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JpaPagingItemReaderParameterTests.java @@ -1,14 +1,15 @@ package org.springframework.batch.item.database; -import org.junit.runner.RunWith; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.batch.item.sample.Foo; -import org.springframework.batch.item.ItemReader; -import org.springframework.beans.factory.annotation.Autowired; -import javax.persistence.EntityManagerFactory; import java.util.Collections; +import javax.persistence.EntityManagerFactory; + +import org.junit.runner.RunWith; +import org.springframework.batch.item.sample.Foo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class JpaPagingItemReaderParameterTests extends AbstractPagingItemReaderParameterTests { @@ -16,7 +17,7 @@ public class JpaPagingItemReaderParameterTests extends AbstractPagingItemReaderP @Autowired private EntityManagerFactory entityManagerFactory; - protected ItemReader getItemReader() throws Exception { + protected AbstractPagingItemReader getItemReader() throws Exception { String jpqlQuery = "select f from Foo f where f.value >= :limit";