Using Kotlin and Spring 5 for some simple project.
I would like to get single record from database by id using queryForObject
.
My query is a 'simple select by id':
jdbc.queryForObject("select id, name from example where id = ?", id)
{ rs: ResultSet, _: Int -> NamedEnt(rs.getLong("id"), rs.getString("name") }
In JdbcOperationsExtensions.kt
it is declared to return nullable type T?
:
fun <T : Any> JdbcOperations.queryForObject(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): T? =
queryForObject(sql, RowMapper { resultSet, i -> function(resultSet, i) }, *args)
In practice when I pass not existing identifier, I face with:
org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
Then I do not understand what is the point of returning nullable type? You either receive a single record or exception. Or I miss something?