Results 1 to 5 of 5

Thread: Possible Bug With JdbcTemplate getObject

  1. #1
    Join Date
    Aug 2004
    Location
    Hawaii, US
    Posts
    225

    Default Possible Bug With JdbcTemplate getObject

    Hello,

    The javadocs say that getObject(String, Object[], Class) will return NULL if the result of the query is NULL.

    I am testing for a NULL in my unit tests, and instead of getting NULL from getObject, I get an exception:

    Code:
    org.springframework.dao.IncorrectResultSizeDataAccessException: Expected single row but found none
    	at org.springframework.jdbc.core.JdbcTemplate$ObjectResultSetExtractor.extractData(JdbcTemplate.java:982)
    	at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:390)
    	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:334)
    	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:375)
    	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:411)
    	at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:470)
    	at com.hic.eboss.dao.BrimsDAOJdbcImpl.getBusinessName(BrimsDAOJdbcImpl.java:99)
    	at
    I think this isn't correct, given the javadocs. I am using a CVS build right before 1.1 release (a few days before 1.1)

    Am I misinterpreting the javadocs?

    Thanks!
    Seth

  2. #2
    Join Date
    Aug 2004
    Location
    Leuven, Belgium
    Posts
    37

    Default

    The javadocs say that getObject(String, Object[], Class) will return NULL if the result of the query is NULL
    You probably mean queryForObject ?

    There is a difference between a query returning NULL and a query returning zero rows.

    zero rows: select salary from emp where 1 = 2
    null result: select max(salary) from emp where 1 = 2

    I guess JdbcTemplate.queryForObject() will throw an exception for the first query and return null for the second query.

    Maarten

  3. #3
    Join Date
    Aug 2004
    Location
    Hawaii, US
    Posts
    225

    Default

    You probably mean queryForObject ?
    Doh! You're right, I'm talking about queryForObject.

    There is a difference between a query returning NULL and a query returning zero rows.
    Ahh... I see. You're right. Thanks for clearing that up for me!

    Seth

  4. #4
    Join Date
    Mar 2007
    Posts
    10

    Default Is this right ?

    public UsuarioVO findUsuarioByLogin(String login) {
    UsuarioVO usuarioVO = null;
    try{
    usuarioVO = (UsuarioVO)getJdbcTemplate().queryForObject("selec t * from where 1 = 2 ",new Object[] {login},new RowMapper());
    }
    catch (IncorrectResultSizeDataAccessException e) {
    usuarioVO = null;
    }
    return usuarioVO;
    }


    should i have to deal with the Exception like this or is there any other better and simple way to retrieve an object ?

    thanks in advance !

  5. #5
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    There are a couple of options; you either find all results and manage the problems of what is returned yourself (e.g. no results, more that one, etc....) or you queryForObject() and manage the exceptions that are thrown. I'd prefer the latter as it handles most of the effort for you.

Similar Threads

  1. Replies: 3
    Last Post: Mar 1st, 2010, 05:45 PM
  2. CMT, JdbcTemplate and connection pools
    By jayschm in forum EJB
    Replies: 3
    Last Post: Apr 25th, 2005, 08:11 AM
  3. Replies: 8
    Last Post: Jan 9th, 2005, 10:24 AM
  4. Injecting JdbcTemplate instead of just DataSource?
    By ArtVandelay in forum Container
    Replies: 4
    Last Post: Oct 20th, 2004, 10:22 PM
  5. Replies: 1
    Last Post: Oct 16th, 2004, 07:07 AM

Posting Permissions

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