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

  • queryForObject EmptyResultDataAccessException

    When I run a queryForObject which returns 0 rows I get a org.springframework.dao.EmptyResultDataAccessExcep tion. At the moment I am running the query in a try/catch and if there is an exception I return the default for that object (e.g. null, empty).

    In a similar thread in another forum the reply from karldmoore a senior member was to use query instead of queryForObject. I guess the idea is to use the size of a list instead of an exception as an indicator.

    Since I am searching for the uid in the query I am expecting 0 or 1 objects to be returned and not a list so using a list still isn't straightforward. Because if I was doing this by hand (not using jdbcTemplate) I would simply use a if( to return the object from persistence or else if no row was returned return the default for that object.

    To me it would be useful to have an additional argument for queryForObject which would be the return object in case of an empty result set?

    public <T> T queryForObject(String sql,
                                Class<T> requiredType,
                                Map args,
                                Object<T> emptyResultObject)
    where emptyResultObject could either be null or a default.

    Does this make sense?


  • #2
    Bumping up this thread - returning null should be standard!


    • #3
      another reason why spring mvc sucks

      Yea. It's real nice how this useless technology throws an ERROR when there are 0 results.