Results 1 to 3 of 3

Thread: frustrated with EmptyResultDataAccessException

  1. #1
    Join Date
    May 2009
    Posts
    7

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Don't use JdbcTemplate then... Use a SqlQuery instance (or one of its subclasses) and use the findObject method...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    May 2009
    Posts
    7

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •