Refine JdbcTemplate nullability contribution

Closes gh-37012
This commit is contained in:
Sébastien Deleuze
2026-07-08 16:52:17 +02:00
parent 516a2ca511
commit 16e82cd693
3 changed files with 42 additions and 4 deletions
@@ -1013,7 +1013,7 @@ public interface JdbcOperations {
* {@link java.sql.Statement#SUCCESS_NO_INFO}/{@link java.sql.Statement#EXECUTE_FAILED})
* @throws DataAccessException if there is any problem issuing the update
*/
int[] batchUpdate(String sql, List<Object[]> batchArgs) throws DataAccessException;
int[] batchUpdate(String sql, List<? extends @Nullable Object[]> batchArgs) throws DataAccessException;
/**
* Execute a batch using the supplied SQL statement with the batch of supplied arguments.
@@ -1026,7 +1026,7 @@ public interface JdbcOperations {
* {@link java.sql.Statement#SUCCESS_NO_INFO}/{@link java.sql.Statement#EXECUTE_FAILED})
* @throws DataAccessException if there is any problem issuing the update
*/
int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes) throws DataAccessException;
int[] batchUpdate(String sql, List<? extends @Nullable Object[]> batchArgs, int[] argTypes) throws DataAccessException;
/**
* Execute multiple batches using the supplied SQL statement with the collect of supplied
@@ -1046,12 +1046,12 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
}
@Override
public int[] batchUpdate(String sql, List<@Nullable Object[]> batchArgs) throws DataAccessException {
public int[] batchUpdate(String sql, List<? extends @Nullable Object[]> batchArgs) throws DataAccessException {
return batchUpdate(sql, batchArgs, new int[0]);
}
@Override
public int[] batchUpdate(String sql, List<@Nullable Object[]> batchArgs, int[] argTypes) throws DataAccessException {
public int[] batchUpdate(String sql, List<? extends @Nullable Object[]> batchArgs, int[] argTypes) throws DataAccessException {
if (batchArgs.isEmpty()) {
return new int[0];
}
@@ -223,4 +223,42 @@ class JdbcOperationsExtensionsTests {
verify { template.query(sql, ofType<RowMapper<Int?>>(), 3) }
}
@Test // gh-37012
fun `batchUpdate with non-null batchArgs`() {
val batchArgs: List<Array<Any>> = listOf(arrayOf(1, "a"), arrayOf(2, "b"))
val result = intArrayOf(1, 1)
every { template.batchUpdate(sql, batchArgs) } returns result
assertThat(template.batchUpdate(sql, batchArgs)).isEqualTo(result)
verify { template.batchUpdate(sql, batchArgs) }
}
@Test // gh-37012
fun `batchUpdate with nullable batchArgs`() {
val batchArgs: List<Array<Any?>> = listOf(arrayOf(1, null), arrayOf(null, "b"))
val result = intArrayOf(1, 1)
every { template.batchUpdate(sql, batchArgs) } returns result
assertThat(template.batchUpdate(sql, batchArgs)).isEqualTo(result)
verify { template.batchUpdate(sql, batchArgs) }
}
@Test // gh-37012
fun `batchUpdate with non-null batchArgs and argTypes`() {
val batchArgs: List<Array<Any>> = listOf(arrayOf(1, "a"), arrayOf(2, "b"))
val argTypes = intArrayOf(JDBCType.INTEGER.vendorTypeNumber, JDBCType.VARCHAR.vendorTypeNumber)
val result = intArrayOf(1, 1)
every { template.batchUpdate(sql, batchArgs, argTypes) } returns result
assertThat(template.batchUpdate(sql, batchArgs, argTypes)).isEqualTo(result)
verify { template.batchUpdate(sql, batchArgs, argTypes) }
}
@Test // gh-37012
fun `batchUpdate with nullable batchArgs and argTypes`() {
val batchArgs: List<Array<Any?>> = listOf(arrayOf(1, null), arrayOf(null, "b"))
val argTypes = intArrayOf(JDBCType.INTEGER.vendorTypeNumber, JDBCType.VARCHAR.vendorTypeNumber)
val result = intArrayOf(1, 1)
every { template.batchUpdate(sql, batchArgs, argTypes) } returns result
assertThat(template.batchUpdate(sql, batchArgs, argTypes)).isEqualTo(result)
verify { template.batchUpdate(sql, batchArgs, argTypes) }
}
}