Announcement Announcement Module
No announcement yet.
frustrated with EmptyResultDataAccessException Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • frustrated with EmptyResultDataAccessException

    as we all know "queryForObject" expects one and only one row to be returned.

    public <T> T queryForObject(String sql,Object[] args,RowMapper<T> rowMapper)throws DataAccessException

    but why isn't there an equivalent signature 'query' method that would return null if there are no rows found ? From what I can tell all of the 'query' methods return a list or I have to use a RowCallbackHandler.

    public <T> T query(String sql,Object[] args,RowMapper<T> rowMapper)throws DataAccessException

    I don't want to implement two mappers or have to check the size of a list. This is really annoying and I don't think totally unreasonable to have a query call that is ok with returning zero objects. Maybe a 'find' call.

    Should I query and count first then if the count = 1 I should queryForObject ?

    Couldn't queryForObject be overloaded to flag if an exception should be found on no results?

  • #2
    Don't use JdbcTemplate then... Use a SqlQuery instance (or one of its subclasses) and use the findObject method...


    • #3
      I suppose, and that is unfortunate. From my perspective the way JdbcTemplate(and operations) should have allowed all 'query' methods to return <T> and allow us to specify the type <T> via our mapper or extractor and not require it to be a list. Lists should have been left to queryForList Now I have to use a completely different paradigm to get the behavior I want.

      It's what happens when frameworks are maintained for backwards compatibility but requirements and processes evolve.

      Maybe I could put in an enhancement request, maybe I will just write my own wrapper.