본문 바로가기
IT/예외처리

Error querying database. Cause: java.lang.IndexOutOfBoundsException

by 최고영회 2022. 6. 15.
728x90
반응형
SMALL

Mybatis + Spring 의 Mapper 이용 시 

 

아래와 같은 오류를 만난다면 

### Cause: java.lang.IndexOutOfBoundsException: Index 18 out of bounds for length 18
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:78)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
	at com.sun.proxy.$Proxy92.selectList(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:223)
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:57)
	at com.sun.proxy.$Proxy93.getAllUserList(Unknown Source)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)

 

resultType 에 정의되어 있는 DTO 클래스에 @Builder 만 덩그러니 있는지 확인해 보자. 

mybatis 의 resultType, resultMap 은 해당 클래스의 객체를 미리 만들어 놓고 맵핑해 주는데 

@Builder 만 있을 경우 쿼리에서 모든 elements 를 select 하지 않을 경우 똑똑하지 못한 mybatis 의 mapper 가 

쿼리에서 가져온 녀석들만 이용해서 생성해주지 못한다. 그래서 IndexOutOfBoundException 이 발생하는 것이다. 

 

@Builder 와 함께 아래와 같이 생성자에 대한 annotation 을 추가해주면 해결

@NoArgsConstructor @AllArgsConstructor

 

 

728x90
반응형
LIST