spring-data-relational icon indicating copy to clipboard operation
spring-data-relational copied to clipboard

ClassCastException on primitive arrays parameters in spring-data-jdbc 2.x.x

Open tanis138 opened this issue 5 years ago • 1 comments

Hi!

Using primitive arrays as named parameters causes ClassCastException in spring-data-jdbc-2.x.x

Simple example (PostgreSQL 8.4+):

    @Query("select unnest( :ids )")
    List<Integer> unnest(Integer[] ids);

    @Query("select unnest( :ids )")
    List<Integer> unnest(int[] ids);

First method works fine, but second throws ClassCastException when casting int[] to Object[]

java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object; 
	at org.springframework.data.jdbc.core.convert.BasicJdbcConverter.writeJdbcValue(BasicJdbcConverter.java:320)

in return JdbcValue.of(typeFactory.createArray((Object[]) convertedValue), JDBCType.ARRAY);

On the other hand, in spring-data-jdbc-1.1.12 both methods work fine.

Tested on spring-data-jdbc-2.1.5 (SB 2.4.3) and spring-data-jdbc-1.1.12 (SB 2.2.13) (Java 8) Configuration:

@Configuration
@EnableJdbcRepositories
public class JdbcConfiguration extends AbstractJdbcConfiguration {
    @Bean
    public NamedParameterJdbcOperations operations(DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

tanis138 avatar Mar 18 '21 07:03 tanis138

On a related note, we should consider the conversion of multi-dimensional arrays.

mp911de avatar Mar 29 '21 12:03 mp911de