문제
ibatis -> mybatis로 마이그레이션 중에 생긴 오류
mybatis에서 selectOne method를 사용해서 받아올때 null로 받아와서 오류가 발생
ibatis는 select method를 사용해서 받아오는데 null 아닌 Map안에 value가 null임
그래서 두개의 차이를 뭔지 파악을 시도
ibatis, mybatis 둘 다 콘솔에 찍히는 SQL문을 실행해보면 동일하게 뜬다
원인
mybatis의 selectOne
@Override
public <T> T selectOne(String statement, Object parameter) {
// Popular vote was to return null on 0 results and throw exception on too many.
List<T> list = this.<T>selectList(statement, parameter);
if (list.size() == 1) {
return list.get(0);
} else if (list.size() > 1) {
throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
} else {
return null;
}
}
ibatis의 select
public Object executeQueryForObject(StatementScope statementScope, Transaction trans, Object parameterObject, Object resultObject)
throws SQLException {
try {
Object object = null;
DefaultRowHandler rowHandler = new DefaultRowHandler();
executeQueryWithCallback(statementScope, trans.getConnection(), parameterObject, resultObject, rowHandler, SqlExecutor.NO_SKIPPED_RESULTS, SqlExecutor.NO_MAXIMUM_RESULTS);
List list = rowHandler.getList();
if (list.size() > 1) {
throw new SQLException("Error: executeQueryForObject returned too many results.");
} else if (list.size() > 0) {
object = list.get(0);
}
return object;
} catch (TransactionException e) {
throw new NestedSQLException("Error getting Connection from Transaction. Cause: " + e, e);
}
}
요약을 하자면은
1. mybatis는 list 자체가 null로 받고있다
2. ibatis는 일련의 과정을 통한 후 rowHandler에서 map에 key는 있고, value는 null을 받아온다.
3. mybatis, ibatis는 list에서 0번째 index를 가져오는건 동일하다.
해결
파악 할 시간이 부족하여서 mybatis에서 받은 후 null 체크를 하여서 Map을 만들어 줘야했다
원인을 제대로 파악을 못한게 아쉽다.
밑의 블로그를 참고하니깐 settings에 아래와 같이 해주면은 null로 받을 수 있다고 한다.
ibatis는 기본적으로 settings이 되어있는거 아니면 설정을 해둔걸로 파악이 된다.
<settings>
<setting name="callSettersOnNulls" value="true"/>
<setting name="returnInstanceForEmptyRow" value="true"/>
</settings>
'개발 > 트러블 슈팅' 카테고리의 다른 글
Github Action에서 ElasticCache 사용 시 gradle build가 안되는 오류 (0) | 2024.01.19 |
---|---|
Jenkins ./gradlew clean build 할 시 gradle Downloading (1) | 2024.01.13 |
[Spring/eGovFrame] interceptor session 안되는, 못가져오는 문제 (0) | 2023.08.22 |
[JSTL] javax.el.PropertyNotFoundException: 타입 [java.lang.String]에서 프로퍼티 [0]을(를) 찾을 수 없습니다. (0) | 2023.06.21 |
No bean named '' available (0) | 2023.06.21 |