'EmptyResultDataAccessException'에 해당되는 글 1건

  1. 2008/11/26 spring2.5.5에서 JdbcTemplate사용시 org.springframework.dao.EmptyResultDataAccessException 발생 (6)
간단하게 DAO를 구현할 일이 생겼다.

spring에 JdbcTemplate를 사용하는 것이 편할거 같아서 이용을 하고 있다.

근데, ResultSet이 없으면  org.springframework.dao.EmptyResultDataAccessException이 발생을 한다.

지라에 등록이 되어 있는데, 오픈이 되어있네.
http://jira.springframework.org/browse/SPR-5294

덧~ 위에 지라에 나온 것은 DB2에 대한 얘기이다.


대략 예외 메세지는 다음과 같다.

org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
at org.springframework.dao.support.DataAccessUtils.requiredSingleResult(DataAccessUtils.java:71)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:722)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:732)
at org.springframework.jdbc.core.simple.SimpleJdbcTemplate.queryForObject(SimpleJdbcTemplate.java:151)
at com.twc.ivr.dao.AccountProfileDaoImpl.findAccount(AccountProfileDaoImpl.java:107)

API를 살펴보니 별 내용이 없다.
http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/dao/EmptyResultDataAccessException.html

그래서 다음과 같이 해결을 했다.

try {
       return (Blog)jdbcTemplate.queryForObject(sql, new Object[] { url }, mapper) ;
} catch (EmptyResultDataAccessException e) {
   return null;
}

구글에서 검색을 해보면. 위와 같은 이유로 고생하는 사람들이 꽤 많다.
http://www.google.co.kr/search?complete=1&hl=ko&newwindow=1&q=JdbcTemplate++org.springframework.dao.EmptyResultDataAccessException&btnG=%EA%B2%80%EC%83%89&lr=&aq=f&oq=

.html 예외는 http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/dao/EmptyResultDataAccessException.html를 서브클래스로 구현되었다.


영회님이 API링크를 줘서 다시 잘 살펴보니 관련 내용이 있다.

queryForObject

public Object queryForObject(String sql,
                             RowMapper rowMapper)
                      throws DataAccessException
Description copied from interface: JdbcOperations
Execute a query given static SQL, mapping a single result row to a Java object via a RowMapper.

Uses a JDBC Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForObject method with null as argument array.

Specified by:
queryForObject in interface JdbcOperations
Parameters:
sql - SQL query to execute
rowMapper - object that will map one object per row
Returns:
the single mapped object
Throws:
IncorrectResultSizeDataAccessException - if the query does not return exactly one row
DataAccessException - if there is any problem executing the query
See Also:
JdbcOperations.queryForObject(String, Object[], RowMapper)
위에 내용에 대해서 덧글을 달아주신 분들이 이야기들을 정리해보면, queryForObject는 하나의 row만 리턴하는 API이고, 쿼리에 만족하는 값이 없으면  IncorrectResultSizeDataAccessException의 sub 클래스인 EmptyResultDataAccessException를 발생한다. 이것은 버그는 아니고 API를 개발한 사람의 의도라는 것이다. 2row이상의 데이터도 아니고 0row를 리턴할때 예외를 발행하는 것은 도데체 알수 없는 의도이다. 어떤 이득을 얻으려고 했을까?

권남님은 그래서 queryForList를 사용하라고 하는데, 내 생각은 하나의 row면 queryForObject를 사용하고 예외처리로 널을 리턴하는 것이 더 좋아보인다. collection이 아닌 데이터를 1개 리턴을 할때 예외가 난다고 리스트로 collection으로 가져온다는 것은 좀 이상하다. 코드에서 의도를 볼때 queryForObject vs queryForList를 사용하는 것은 의미적 차이가 크다.

http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/JdbcTemplate.html

Posted by ologist
이전버튼 1 이전버튼

블로그 이미지
ologist

공지사항

Yesterday221
Today25
Total34,406

달력

 « |  » 2012.02
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

최근에 받은 트랙백

글 보관함