Monday, May 30, 2011

EmptyResultDataAccessException in your face

Recently I get an EmptyResultDataAccessException in a project where we use Spring JDBC. The full exception listing is "org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0". When I trace the exception I see that I'm calling queryForLong() method of JdbcTemplate for "SELECT Column FROM TABLENAME" on an empty table. Column here is of Long type. Now, I plan to catch the exception and return an unusual value (like -1) as a result. On the service layer, I plan to check for equality to the unusual value and proceed with the right logical flow.

Notice that if you use some function like count(), min(), max() ... in the same query (SELECT MAX(Column) FROM TABLENAME) you won't get the same exception but a "0" result instead.