JdbcCursorItemReader 를 이용해, db 쿼리를 한 뒤에, 조인 된 데이터를 가져와서 DTO data class에 담아 처리를 해야 할 일이 있었습니다.
기존
return JdbcCursorItemReaderBuilder<WithRewardAmountDto>()
.name("xxxStepItemReader")
.dataSource(secondaryDataSource)
.sql(
"""
SELECT .. xxx 비밀 ^^
""".trimIndent(),
)
.fetchSize(BatchConstant.STEP_CHUNK_SIZE)
.rowMapper(BeanPropertyRowMapper(WithRewardAmountDto::class.java))
.build()
위 처럼 쓰고 있는데, 오류가 발생 합니다.
오류 상황
Caused by: java.lang.IllegalArgumentException: No argument provided for a required parameter: parameter #0 id of fun `<init>`(kotlin.Long, kotlin.String, kotlin.Long, java.math.BigDecimal, java.time.OffsetDateTime): com.seungdols.company.dto.WithRewardAmountDto
org.springframework.beans.BeanInstantiationException: Failed to instantiate [WithRewardAmountDto]: Illegal arguments for constructor
확인을 해보니, 호환성 문제가 있었고 해결 방법은 간단하게는 3가지가 있습니다. 물론, 위 이슈는 Java의 Record 타입을 쓰더라도 발생합니다.
해결방안
- data class를 class 로 변경한다.
- BeanPropertyRowMapper 대신 DataClassRowMapper 사용한다.
- Custom으로 RowMapper를 구현한다. (성능이 주요하다면, !?) - ref. docs
쉬운 방법으로 변경 하게 됐는데, 2번을 택했습니다.
반응형
'프로그래밍 > Kotlin' 카테고리의 다른 글
Kotlin + JPA 의 val id 이슈 (0) | 2025.01.21 |
---|---|
Rest API와 Grpc API 서버를 하나로 서비스 할 수 있다고?! (5) | 2024.08.28 |
kotlin - jackson 관련 이슈 (토비의 스프링 6 강의) (0) | 2024.07.05 |
ktlint lint 적용 (0) | 2024.02.08 |
kotest 관련 @Transactional rollback 안되는 이슈 (0) | 2023.12.14 |