Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tanis138 opened this issue Mar 18, 2021 · 1 comment
Closed
Assignees
Labels
type: bug A general bug

Comments

@tanis138
Copy link

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);
    }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 18, 2021
@schauder schauder added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 22, 2021
@schauder schauder self-assigned this Mar 22, 2021
schauder added a commit that referenced this issue Mar 23, 2021
schauder added a commit that referenced this issue Mar 23, 2021
schauder added a commit that referenced this issue Mar 23, 2021
mp911de pushed a commit that referenced this issue Mar 29, 2021
mp911de added a commit that referenced this issue Mar 29, 2021
Refactor ArrayUtil into a proper utility class providing toPrimitive and toObject methods for each primitive type. Add boolean support. Move convertToObjectArray to BasicJdbcConverter as its placement in ArrayUtils creates a certain amount of ambiguity over its actual purpose.

Create unit test.

See #945
Original pull request: #949.
mp911de added a commit that referenced this issue Mar 29, 2021
Refactor ArrayUtil into a proper utility class providing toPrimitive and toObject methods for each primitive type. Add boolean support. Move convertToObjectArray to BasicJdbcConverter as its placement in ArrayUtils creates a certain amount of ambiguity over its actual purpose.

Create unit test.

See #945
Original pull request: #949.
mp911de pushed a commit that referenced this issue Mar 29, 2021
mp911de added a commit that referenced this issue Mar 29, 2021
Refactor ArrayUtil into a proper utility class providing toPrimitive and toObject methods for each primitive type. Add boolean support. Move convertToObjectArray to BasicJdbcConverter as its placement in ArrayUtils creates a certain amount of ambiguity over its actual purpose.

Create unit test.

See #945
Original pull request: #949.
@mp911de mp911de added this to the 2.0.9 (Neumann SR9) milestone Mar 29, 2021
@mp911de
Copy link
Member

mp911de commented Mar 29, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants